我想将 JSONObject 添加到 JSONArray 并且该 JSONArray 包含在其他 JSONObject 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7497980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:59:57  来源:igfitidea点击:

I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject

jsonarrays

提问by Ajka

I need below kind of structure constructed in java and send it as response :

我需要以下在java中构造的结构并将其作为响应发送:

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

I am doing:

我在做:

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

and herer above function is converting paramMap into json string like:

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

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

but it is not creating the right structure, can anybody help me in this?

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

here is what I am getting output:

这是我得到的输出:

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

which is not being parsed in javascript.

这不是在 javascript 中解析的。

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

last alert showing undefined.

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



ohh sorry I forgot to paste last line , here is the last line:

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

getJSONMessage(paramMap);

and above function is converting paramMap into json string:

以上函数将 paramMap 转换为 json 字符串:

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

回答by Gon?alo Vieira

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

Why were you using an Hashmap if what you wanted was to put it into a JSONObject?

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

EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html

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

EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327, that method is not deprecated.

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

We're storing a string representation of the JSONObject, not the JSONObject itself

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

回答by Vishnu Mari

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

回答by sagar varade

JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());

This works for me.

这对我有用。

回答by Nikhil

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