2012-08-27 13 views
21

Dziś dużo googlowałem do tego tematu. Ale nie mogę go znaleźć, Jak mogę dodać JSONArray do JSONObject?Dodaj JsonArray do JsonObject

Ponieważ za każdym razem to zrobić otrzymuję ten błąd: 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;   
} 

otrzymuję ten błąd:

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) 

JSON chcę: Tylko ostatni JsonArray idzie źle:

{ 
      "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": [ { forma: '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 .. Także osoba, która mu odpowiedziała, jego opcja nie działa dla mnie! – Gynnad

+1

Myślę, że to może być błąd w implorze json, którego używasz. wygląda tak, jakbyś poprawnie używał interfejsu API z podanego kodu. – jtahlborn

Odpowiedz

32

myślę, że jest to problem (aka. bug) z używanym API. JSONArray implementuje Collection (implementacja json.org, z której pochodzi ten interfejs, powoduje, że , a nie, kolekcja narzędzi JSONArray). I JSONObject ma przeciążoną metodę put(), która pobiera kolekcję i owija ją w JSONArray (powodując w ten sposób problem). Myślę, że trzeba zmusić inną metodę JSONObject.put() być stosowane:

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

Powinieneś zgłosić błąd z dostawcą, całkiem pewny sposób ich JSONObject.put(String,Collection) jest zepsuty.

+0

Dzięki, to jest rozwiązanie! :) – Gynnad

+0

bardzo ładne .. rozwiązanie ... –

20

tutaj jest prosty kod

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

co nie było w stanie odejść? –

+0

Obiekt PrintWriter –

1

Zaczynam uczyć się o tym sam, będąc bardzo nowy android rozwoju i znalazłem ten film bardzo pomocne.

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

Obejmuje ona konkretnie dostać JSONArray do JSONObject godzinie 19:30 w filmie.

Kod z teledysku do JSONArray do 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

Można po prostu zrobić to za pomocą JSON prosta biblioteka. Oto Gradle

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

Oto przykładowy kod:

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); 

To wszystko.:)

+0

Jak odzyskać nazwy tablic przechowywane w obiekcie JSON? –

2

Twoja lista:

List<MyCustomObject> myCustomObjectList; 

Twój JSONArray:

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

Twoja odpowiedź:

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

Twój post/put prośba ciało http byłoby tak:

{ 
     "yourCustomKey: [ 
      { 
       "myCustomObjectProperty": 1 
      }, 
      { 
       "myCustomObjectProperty": 2 
      } 
     ] 
    } 
Powiązane problemy