今天我在谷歌上搜索了很多关于这个话题的信息.但我找不到它,我如何才能将JSONArray添加到JSONObject?

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)

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

        {
            formaat: 'vierkant45x15'
            tegels: [
                    {naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
                    ,{naam: '', imgThumb: '/bla/bla.png', largeImg: '/bla/bla2.png'}
                    ]
        }
    ]

}

推荐答案

我认为这是一个问题(又名.错误)和您正在使用的API.JSONArray实现Collection(派生此API的json.org实现not具有JSONArray实现集合).和JSONObject具有重载的put()方法,该方法获取集合并将其包装在JSONArray中(从而导致问题).我认为您需要强制使用其他JSONObject.put()种方法:

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

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

Json相关问答推荐

使用WSO2 JsonTransform Mediator对空值执行JsonExceptionUndeletedOperationException

创建Json嵌套文件 struct

PowerShell:使用JSON原生的Short命令处理JSON?

重构JOLT代码以获得预期输出

递归解码嵌套列表(具有任意深度的列表列表)

基于 JSON 字段的 Jolt 条件标志

json 字符串到 Postgres 14 中的表视图

如何使用jolt将嵌套数据变成线性数据

如何迭代、动态加载我的表单输入元素,然后在 React 中的表单提交上检索输入值?

如何实现一个通用的 serde_json::from_str

通过一个序列化器更新多个模型数据

使用 BASH 和 JQ 我想将 json 文件与 bash 数组进行比较

将字符串映射到json对象的多种类型?

android - 在 adb logcat 输出中格式化 json 字符串

如何在java中比较来自JsonObject的空值

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

如何从 MySQL 中检索 JSON 数据?

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

从动态 json 数据更新力有向图上的链接

如何在 postgresql 9.3 中循环 JSON 数组