I googled a lot today for this subject. But I can't find it, How can I add a JSONArray to a JSONObject?
Because everytime I do this I get this error: Stackoverflow
JSONObject fillBadkamerFormaatFromContentlet(Structure structure, String formaat) { JSONObject jsonObject = new JSONObject(); JSONArray arr = new JSONArray(); BadkamerFormaat badkamerFormaat = new BadkamerFormaat(); BadkamerTegel badkamerTegel; List<Contentlet> contentlets = getContentletsByStructure(structure); badkamerFormaat.formaat = formaat; badkamerFormaat.tegels = new ArrayList<BadkamerTegel>(); try { jsonObject.put("formaat", formaat); } catch (JSONException e1) { throw new RuntimeException(e1); } for(Contentlet contentlet : contentlets) { badkamerTegel = new BadkamerTegel(); badkamerTegel.naam = contentlet.getStringProperty(ParameterNames.toolBetegelVeldNaam); try { badkamerTegel.afbeeldingTegel = contentlet.getBinary(ParameterNames.toolBetegelVeldTegelAfbeelding).getPath(); badkamerTegel.afbeeldingBadkamer = contentlet.getBinary(ParameterNames.toolBetegelVeldBadkamerAfbeelding).getCanonicalPath(); arr.put(badkamerTegel.toJSON()); } catch (IOException e) { throw new RuntimeException(e); } } try { jsonObject.put("aoColumnDefs",arr); } catch (JSONException e) { throw new RuntimeException(e); } return jsonObject; }
I get this error:
java.lang.StackOverflowError at com.dotmarketing.util.json.JSONArray.<init>(JSONArray.java:248) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953) at com.dotmarketing.util.json.JSONObject.put(JSONObject.java:953)
The JSON I want: Only the last JsonArray is going wrong:
{ "wand": [ { formaat: 'vierkant15x15' tegels: [ {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ] } , { formaat: 'vierkant17x15' tegels: [ {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ] } ]
, "vloer": [ { formaat: 'vierkant10x15' tegels: [ {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ] } ,
{ formaat: 'vierkant45x15' tegels: [ {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'} ] } ]
}
We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.
JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);
JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.
I think it is a problem(aka. bug) with the API you are using. JSONArray
implements Collection
(the json.org implementation from which this API is derived does not have JSONArray implement Collection). And JSONObject
has an overloaded put()
method which takes a Collection and wraps it in a JSONArray
(thus causing the problem). I think you need to force the other JSONObject.put()
method to be used:
jsonObject.put("aoColumnDefs",(Object)arr);
You should file a bug with the vendor, pretty sure their JSONObject.put(String,Collection)
method is broken.
here is simple code
List <String> list = new ArrayList <String>(); list.add("a"); list.add("b"); JSONArray array = new JSONArray(); for (int i = 0; i < list.size(); i++) { array.put(list.get(i)); } JSONObject obj = new JSONObject(); try { obj.put("result", array); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } pw.write(obj.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With