Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JSON in java using org.json

I am trying to create a json string in java using org.json library and following is the code snippet.

JSONArray jSONArray = new JSONArray();
JSONObject jSONObject = new JSONObject();
jSONObject.accumulate("test", jSONArray);
System.out.println(jSONObject.toString());

I expected it to print

{"test":[]} 

while it prints

{"test":[[]]}
like image 651
raju Avatar asked Apr 29 '13 05:04

raju


1 Answers

instead of using accumulate use put this way it won;t add it to a pre-existing (or create and add) JSONArray, but add it as a key to the JSONObject like this:

JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("test", array);
System.out.println(obj.toString());

and now it'll print {"test":[]}

like image 72
thepoosh Avatar answered Sep 22 '22 19:09

thepoosh