Here is the hack
ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree("{\"name\":[\"bishal\",\"jaiswal\",\"Turtle\"],\"title\":\"jaiswal\"}");
final JsonNode name = jsonNode.get("name");
final Iterator<JsonNode> iterator = name.iterator();
StringBuilder s = new StringBuilder();
while (iterator.hasNext()){
final JsonNode next = iterator.next();
if(s.length() > 0){
s.append(",");
}
s.append(next);
}
System.out.println(s.toString());
Hope so it will help you.
You can use Jackson JSON in this case...
Jackson is a very popular and efficient java based library to serialize or map Java objects to JSON and vice versa
For e.g.
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonData); //jsonData is your json string here...
JsonNode names = rootNode.path("name");
Iterator<JsonNode> elements = names.elements();
while(elements.hasNext()){
JsonNode name = elements.next();
System.out.println("Names are = "+name);
}
Please can you let me know if this has solved your problem? Thanks
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)
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.
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);