2012-08-27 20 views
21

Ich googelte heute viel für dieses Thema. Aber ich kann es nicht finden. Wie kann ich ein JSONArray zu einem JSONObject hinzufügen?Fügen Sie JsonArray zu JsonObject hinzu

Denn jedes Mal wenn ich das mache ich diesen Fehler: 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;   
} 

ich diesen Fehler:

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) 

Die JSON Ich möchte: Nur die letzte JsonArray schief läuft:

{ 
      "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": [ { format: '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'} 
        ] 
     } 
    ] 

}

+0

Hmm .. Auch die Person, die er antwortete, seine Option funktionierte nicht für mich! – Gynnad

+1

Ich denke, es könnte ein Fehler in der JSON Impl sein, die Sie verwenden. Es sieht so aus, als ob Sie die API korrekt aus dem von Ihnen bereitgestellten Code verwenden. – jtahlborn

Antwort

32

ich denke, es ist ein Problem (aka. Fehler) mit der API, die Sie verwenden. JSONArray implementiert Collection (die json.org-Implementierung, von der diese API abgeleitet ist, hat nicht haben JSONArray implementieren Collection). Und JSONObject hat eine überladene put() Methode, die eine Sammlung nimmt und sie in eine JSONArray umwickelt (wodurch das Problem verursacht wird). Ich denke, dass Sie die andere JSONObject.put() Methode zwingen müssen, um zu verwenden:

jsonObject.put("aoColumnDefs",(Object)arr); 

Sie sollten einen Fehler mit dem Anbieter-Datei, ziemlich sicher, dass ihre JSONObject.put(String,Collection) Methode gebrochen ist.

+0

Danke, das ist die Lösung! :) – Gynnad

+0

sehr schön .. lösung ... –

20

hier ist einfach 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()); 
+0

was ist pw du hast nicht dekleare? –

+0

PrintWriter-Objekt –

1

Ich fange an, über diese selbst zu lernen, ist sehr neu für Android-Entwicklung und ich fand das Video sehr hilfreich.

https://www.youtube.com/watch?v=qcotbMLjlA4

Es umfasst speziell auf JSONArray zu JSONObject um 19:30 Uhr im Video zu bekommen.

-Code aus dem Video für JSONArray zu JSONObject:

JSONArray queryArray = quoteJSONObject.names(); 

ArrayList<String> list = new ArrayList<String>(); 

for(int i = 0; i < queryArray.length(); i++){ 
    list.add(queryArray.getString(i)); 
} 

for(String item : list){ 
    Log.v("JSON ARRAY ITEMS ", item); 
} 
0

Sie können einfach tun es Json Einfache Bibliothek. Hier ist die gradle

compile 'com.googlecode.json-simple:json-simple:1.1'

Hier ist ein Beispielcode:

org.json.simple.JSONObject jsonObject=new org.json.simple.JSONObject(); 
jsonObject.put("Object","String Object"); 

ArrayList<String> list = new ArrayList<String>(); 
      list.add("john"); 
      list.add("mat"); 
      list.add("jason"); 
      list.add("matthew"); 

      jsonObject.put("List",list); 

Das ist es.:)

+0

Wie können die im JSON-Objekt gespeicherten Array-Namen abgerufen werden? –

2

Ihre Liste:

List<MyCustomObject> myCustomObjectList; 

Ihre JSONArray:

// Don't need to loop through it. JSONArray constructor do it for you. 
new JSONArray(myCustomObjectList) 

Ihre Antwort:

return new JSONObject().put("yourCustomKey", new JSONArray(myCustomObjectList)); 

Ihre Post/put http Körper Anfrage so sein würde:

{ 
     "yourCustomKey: [ 
      { 
       "myCustomObjectProperty": 1 
      }, 
      { 
       "myCustomObjectProperty": 2 
      } 
     ] 
    } 
Verwandte Themen