Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add JsonArray to JsonObject

Tags:

java

json

arrays

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'}                     ]         }     ] 

}

like image 649
Danny Gloudemans Avatar asked Aug 27 '12 12:08

Danny Gloudemans


People also ask

How can we add a JSONArray to JSON object in Java?

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.

Is JSONArray a JSON object?

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.

How do I add a list to a JSON object?

JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);

How add JSON array to another JSON array?

JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.


2 Answers

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.

like image 119
jtahlborn Avatar answered Sep 17 '22 19:09

jtahlborn


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()); 
like image 32
Ravi Avatar answered Sep 17 '22 19:09

Ravi