Wrap the string in "[]" and then use the JSON parser of your choice.
Wrap the string in "[]" and then use the JSON parser of your choice.
There is Java Lib called Quick-json - A java library to convert json string to java objects & java objects to json string. parse/generate json the way you want quickly. configure/validate json at every level.
Please refer for Quick- Json Lib https://code.google.com/p/quick-json/ has good example's as well as explanation.
For such a trivial example, it's pretty easy to produce a poorly-formed JSON String of your content and let JSONObject patch it up.
In a single expression:
new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:$2,")))
// {"art":0,"comedy":0,"action":0,"crime":0,"animals":0}
If you really want to keep the 0.0 as Strings:
new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:\"$2\",")))
// {"art":"0.0","comedy":"0.0","action":"0.0","crime":"0.0","animals":"0.0"}
If you want to account for possible extraneous whitespaces:
new JSONObject(String.format("{%s}", str.replaceAll("([^,]+)\\s*?,\\s*?([^,]+)(,|$)", "$1:$2,")))
.. will work with inputs like "art, 0.0, comedy, 0.0, action, 0.0, crime, 0.0, animals, 0.0" and other cases.
Disclaimer: it's not crazy-sexy code but drop in a one-line comment and it could be reasonable so long as the data structure stays simplistic.
You should use Map instead of Array (List of key/value pair is Map, not Array).
1 way: Using JsonObject
String [] arrayStr=strVal.split(",");
JsonObjectBuilder builder = Json.createObjectBuilder()
String key = null;
for (String s: arrayStr){
if(key == null) {
key = s;
} else {
builder.add(key, s);
key = null;
}
}
JsonObject value = builder.build();
2 way: Using Map
String [] arrayStr=strVal.split(",");
Map<String,String> map = new HashMap<>();
String key = null;
for (String s: arrayStr){
if(key == null) {
key = s;
} else {
map.put(key, s);
key = null;
}
}
// convert Map to Json using any Json lib (Gson, Jackson and so on)
If you don't care about the order of the object members:
$ perl -CO -MJSON::PP -l -0777 -ne '
$j = decode_json($_);
print join ",", map("$_=$j->{$_}", keys %$j)' your-file
var3=789,var2=456,var1=123
perl comes pre-installed on most systems, and JSON::PP is one its built-in modules.
Or if your system has python3:
PYTHONIOENCODING=utf-8 python3 -c '
import json, sys;
_ = json.load(sys.stdin);
print(",".join(map(lambda i: "%s=%s" % i, _.items())))' < your-file
(the order should be preserved as python3's dictionaries contrary to perl's hashes do keep track of an order of keys).
With a less tamed input such as:
{
"var-\"ⓧ\"": 1e-2,
"var=v,v": "\n",
"var\uD83E\uDDD9": null
}
They give
var=v,v=
,var-"ⓧ"=0.01,var=
And
var-"ⓧ"=0.01,var=v,v=
,var=None
Respectively, while mlr --ijson cat gives:
var-"ⓧ"=1e-2,var=v,v=
,var=null
And jq -r 'to_entries|map("\(.key)=\(.value)")|join(",")' gives:
var-"ⓧ"=0.01,var=v,v=
,var=null
(though neither jq nor mlr are generally installed by default).
If it will always be this simple, something like this might work
tr : = | tr -d '{}"\n ' | sed -e "s/^/'/" -e "s/$/'/"
In parts:
- translate
:to= - remove all other extra characters including punctuation, spaces, newlines
- add single quote at start and end
Either write a simple method yourself, or use one of the various utilities out there.
Personally I use apache StringUtils (StringUtils.join)
edit: in Java 8, you don't need this at all anymore:
String joined = String.join(",", name);
Android developers are probably looking for TextUtils.join
Android docs: http://developer.android.com/reference/android/text/TextUtils.html
Code:
String[] name = {"amit", "rahul", "surya"};
TextUtils.join(",",name)
Simple:
var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
return val.location_id;
}).join(',');
console.log(result)
I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.
You could add brackets to the string, parse the string (JSON.parse) and map (Array#map) the property and the join (Array#join) the result.
var string = '{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}',
array = JSON.parse('[' + string + ']'),
result = array.map(function (a) { return a.location_id; }).join();
console.log(result);