添加到JsonArray的JSONObject [英] Add JsonArray to JsonObject

查看:214
本文介绍了添加到JsonArray的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我GOOGLE了很多关于这个主题。但我不能找到它,我如何添加一个JSONArray到一个JSONObject?

I googled a lot today for this subject. But I can't find it, How can I add a JSONArray to a JSONObject?

由于每次我这样做,我得到这个错误:#1

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)

的JSON我想:只有最后JsonArray是怎么了?

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

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

}

推荐答案

我觉得这是一个问题(亦称BUG)的的API 你正在使用。 JSONArray 工具收藏(在json.org从这个API是派生实现确实的的有JSONArray实现Collection)。和的JSONObject 有一个重载把()方法,这需要收集并把它封装在一个 JSONArray (从而导致问题)。我认为你需要迫使对方 JSONObject.put()要使用的方法:

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

您应该提交一个bug与供应商,pretty确保他们的 JSONObject.put(字符串,集合)方法被打破了。

You should file a bug with the vendor, pretty sure their JSONObject.put(String,Collection) method is broken.

这篇关于添加到JsonArray的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆