向 JsonObject 添加 JsonArray

36

今天我在谷歌上搜索了很多关于这个问题的内容,但是我找不到答案。我该如何将一个JSONArray添加到一个JSONObject中呢?每次尝试时都会出现这个错误: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;          
}

我遇到了这个错误:

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

}


这是一个简单的HTML段落,其中包含右花括号 "}"。

嗯...还有那个回答他的人,他的选项对我没用! - Danny Gloudemans
1
我认为你正在使用的JSON实现可能有一个Bug。从你提供的代码来看,你似乎是正确地使用了API。 - jtahlborn
5个回答

41

我认为这是您正在使用的API存在的问题(即bug)。 JSONArray 实现了 Collection (此 API 源自 json.org ,该实现未使 JSONArray 实现 Collection)。而 JSONObject 有一个重载的 put() 方法,它接受一个 Collection 并将其包装在一个 JSONArray 中(从而导致问题发生)。我认为您需要强制使用其他的 JSONObject.put() 方法:

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

你应该向供应商报告一个错误,很确定他们的JSONObject.put(String,Collection)方法有问题。


28

这里是简单的代码

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

PrintWriter 对象 - user1999397

7

您的列表:

List<MyCustomObject> myCustomObjectList;

您的JSONArray:

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

Your response:

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

您的post/put http请求体应该像这样:
    {
        "yourCustomKey: [
           {
               "myCustomObjectProperty": 1
           },
           {
               "myCustomObjectProperty": 2
           }
        ]
    }

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

尝试以下简单的解决方案:

JsonObject body=new JsonObject();
body.add("orders", (JsonElement) orders);

每当我的JSON请求是这样的:
{
      "role": "RT",
      "orders": [
        {
          "order_id": "ORDER201908aPq9Gs",
          "cart_id": 164444,
          "affiliate_id": 0,
          "orm_order_status": 9,
          "status_comments": "IC DUE - Auto moved to Instruction Call Due after 48hrs",
          "status_date": "2020-04-15",
        }
      ]
    }

1
ClassCastException:java.util.ArrayList无法转换为com.google.gson.JsonElement - roghayeh hosseini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接