【问题标题】:Add JsonArray to JsonObject将 JsonArray 添加到 JsonObject
【发布时间】:2012-08-21 22:57:02
【问题描述】:

我今天为这个主题搜索了很多。但是我找不到,如何将 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”:[ { 格式:'virkant10x15' 泰格尔斯:[ {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'}
                    ]
        }
    ]

}

【问题讨论】:

  • 嗯.. 也是回答他的人,他的选择对我不起作用!
  • 我认为这可能是您正在使用的 json impl 中的错误。它看起来从您提供的代码中正确使用了 API。

标签: java json arrays


【解决方案1】:

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

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

您应该向供应商提交错误,很确定他们的 JSONObject.put(String,Collection) 方法已损坏。

【讨论】:

  • 谢谢,这就是解决方案! :)
【解决方案2】:

这里是简单的代码

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 对象
【解决方案3】:

您的清单:

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

您的 post/put http 正文请求将是这样的:

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

【讨论】:

    【解决方案4】:

    我自己开始了解这一点,对 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);
    }
    

    【讨论】:

      【解决方案5】:

      试试下面一个简单的解决方案:

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

      【讨论】:

      • ClassCastException: java.util.ArrayList 无法转换为 com.google.gson.JsonElement
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2016-12-03
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多