【问题标题】:I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject我想将 JSONObject 添加到 JSONArray 并且 JSONArray 包含在其他 JSONObject 中
【发布时间】:2011-09-21 10:02:51
【问题描述】:

我需要以下用 java 构建的结构并将其作为响应发送:

var abc = {
  "action": "Remove",
  "datatable": [
    { "userid": "userid0", "username": "name0" },
    { "userid": "userid1", "username": "name1" },
    { "userid": "userid2", "username": "name2" },
    { "userid": "userid3", "username": "name3" }
  ],
  "msgType": "success"
};

我在做:

JSONArray jsonArray = new JSONArray();

for (loop) {
    JSONObject jsonObj= new JSONObject();
    jsonObj.put("srcOfPhoto", srcOfPhoto);
    jsonObj.put("username", "name"+count);
    jsonObj.put("userid", "userid"+count);

    jsonArray.add(jsonObj.toJSONString());
}

Map paramMap = new HashMap();

paramMap.put("action", "remove");

paramMap.put("datatable", jsonArray );

paramMap.put(Constant.MSG_TYPE , Constant.SUCCESS);

getJSONMessage(paramMap);

上面的函数是将 paramMap 转换为 json 字符串,如:

public static String getJSONMessage(Map<String, String> paramMap) {
    if (paramMap != null && paramMap.size() > 0)
        return JSONObject.toJSONString(paramMap);
    else
        return "";
}

但它没有创建正确的结构,有人可以帮助我吗?

这是我得到的输出:

{"action":"Remove","datatable":[{\"userid\":\"userid0\",\"srcOfPhoto\":\"users\\\/JMMBGTCHG.jpg\",\"username\":\"name0\"}"],"msgType":"success"}

没有被 javascript 解析。

var json = eval('(' + respText+')');
alert("contents>>"+json.datatable);
alert("contents.datatable[0]>>>"+json.datatable[0].username);

最后一个警报显示未定义。


对不起,我忘了贴最后一行,这是最后一行:

getJSONMessage(paramMap);

上面的函数是将paramMap转换成json字符串:

public static String getJSONMessage(Map<String, String> paramMap){
    if(paramMap != null && paramMap.size() > 0)
        return JSONObject.toJSONString(paramMap);
    else
        return "";
}

【问题讨论】:

  • 我已将您的两个帐户合并在一起。 Please read this Faq entry about cookie-based accounts. 另外,StackOverflow 不是论坛;如果您有新问题,请提出新问题。如果您想在问题中包含更多信息,请edit it。如果您想与其中一位已回答的人互动,可以给他们留言。

标签: json arrays


【解决方案1】:
JSONArray jsonArray = new JSONArray();

for (loop) {
    JSONObject jsonObj= new JSONObject();
    jsonObj.put("srcOfPhoto", srcOfPhoto);
    jsonObj.put("username", "name"+count);
    jsonObj.put("userid", "userid"+count);

    jsonArray.put(jsonObj.valueToString());
}

JSONObject parameters = new JSONObject();

parameters.put("action", "remove");

parameters.put("datatable", jsonArray );

parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);

如果您想要将 Hashmap 放入 JSONObject 中,为什么还要使用它?

编辑:根据http://www.json.org/javadoc/org/json/JSONArray.html

EDIT2:在所使用的 JSONObject 方法上,我遵循以下代码:https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327,该方法未被弃用。

我们存储的是 JSONObject 的字符串表示,而不是 JSONObject 本身

【讨论】:

  • 没有add(jsonObj),但put(jsonObj)是可能的(似乎也不需要转换成JSONString)
  • @SomeoneSomewhere:有添加方法:
  • JSONObject 类型的 toJSONString() 方法未定义??
  • 看来标准改成valueToString()会编辑。
  • jsonArray 对象中没有 push() 方法。
【解决方案2】:
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());

这对我有用。

【讨论】:

    【解决方案3】:
    JSONObject json = new JSONObject();
    json.put("fromZIPCode","123456"); 
    
    JSONObject json1 = new JSONObject();
    json1.put("fromZIPCode","123456"); 
           sList.add(json1);
           sList.add(json);
    
    System.out.println(sList);
    
    Output will be
    
    [{"fromZIPCode":"123456"},{"fromZIPCode":"123456"}]
    

    【讨论】:

    • 我认为这不能回答问题。
    • 这与问题完全无关。
    【解决方案4】:
    org.json.simple.JSONArray resultantJson = new org.json.simple.JSONArray();
    
            org.json.JSONArray o1 = new org.json.JSONArray("[{\"one\":[],\"two\":\"abc\"}]");
            org.json.JSONArray o2 = new org.json.JSONArray("[{\"three\":[1,2],\"four\":\"def\"}]");
    
    
            resultantJson.addAll(o1.toList());
            resultantJson.addAll(o2.toList());
    

    【讨论】:

    • 请尝试解释为什么您认为这段代码可以解决问题。
    【解决方案5】:
    package com.JsonArray;
    
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    
    public class WriteJsonArray {
        public static void main(String[] args) {
    
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("action","Remove");
            jsonObject.addProperty("msgType","success");
    
    
            JsonObject item1 = new JsonObject();
            item1.addProperty("userid", "userid0");
            item1.addProperty("username","name0");
    
            JsonObject item2 = new JsonObject();
            item2.addProperty("userid", "userid1");
            item2.addProperty("username","name1");
    
            JsonObject item3 = new JsonObject();
            item3.addProperty("userid", "userid2");
            item3.addProperty("username","name2");
    
            JsonObject item4 = new JsonObject();
            item4.addProperty("userid", "userid3");
            item4.addProperty("username","name3");
    
            JsonArray jsonArray = new JsonArray();
            jsonArray.add(item1);
            jsonArray.add(item2);
            jsonArray.add(item3);
            jsonArray.add(item4);
            jsonObject.add("datatable", jsonArray);
    
            System.out.println("Result: \n" + jsonObject.toString());
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多