2012-08-27 53 views
21

今天我爲這個主題搜索了很多。但是我找不到它,我如何將JSONArray添加到JSONObject?將JsonArray添加到JsonObject

因爲每次我這樣做,我得到這個錯誤:#1

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

我得到這個錯誤:

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:只有最後JsonArray是哪裏錯了:

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

}

+0

嗯..還有誰回答他的人,他的選擇不適合我! – Gynnad

+1

我認爲這可能是您使用的json impl中的一個錯誤。它看起來像你從你提供的代碼中正確使用API​​。 – jtahlborn

回答

32

我認爲這是一個問題(又名。錯誤)與您正在使用的APIJSONArray implements Collection(從這個API派生的json.org實現而不是有JSONArray實現集合)。並且JSONObject有一個超載的put()方法需要一個集合並將其封裝在一個JSONArray(因此導致該問題)。我認爲你需要迫使對方JSONObject.put()方法中使用:

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

你應該提交一個bug與供應商,很肯定他們JSONObject.put(String,Collection)方法被打破。

+0

謝謝,這是解決方案! :) – Gynnad

+0

很好..解決方案... –

20

這裏是簡單的代碼

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

你沒有解密什麼是pw? –

+0

PrintWriter對象 –

1

我開始瞭解這個自己,是很新的Android開發,我發現這段視頻非常有幫助。

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

它具體涉及到在視頻19:30拿到JSONArray到JSONObject的。從視頻JSONArray到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

可以根本就使用JSON簡單的庫吧。 這裏是gradle這個

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

下面是示例代碼:

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

就是這樣。:)

+0

如何檢索存儲在JSON對象中的數組名稱? –

2

列表:

List<MyCustomObject> myCustomObjectList; 

你JSONArray:

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

您的迴應:

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

您的文章/ PUT HTTP請求的身體會是這樣:

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