Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想将一个JSONObject添加到JSONArray中,并将该JSONArray包含在其他JSONObject中_Json_Arrays - Fatal编程技术网

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

我想将一个JSONObject添加到JSONArray中,并将该JSONArray包含在其他JSONObject中,json,arrays,Json,Arrays,我需要以下用java构建的结构,并将其作为响应发送: var abc={ “操作”:“删除”, “数据表”:[ {“userid”:“userid0”,“username”:“name0”}, {“userid”:“userid1”,“username”:“name1”}, {“userid”:“userid2”,“username”:“name2”}, {“userid”:“userid3”,“username”:“name3”} ], “msgType”:“成功” }; 我正在做: JSO

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

var abc={
“操作”:“删除”,
“数据表”:[
{“userid”:“userid0”,“username”:“name0”},
{“userid”:“userid1”,“username”:“name1”},
{“userid”:“userid2”,“username”:“name2”},
{“userid”:“userid3”,“username”:“name3”}
],
“msgType”:“成功”
};
我正在做:

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 "";
}
它没有在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 "";
}
public静态字符串getJSONMessage(Map-paramMap){
if(paramMap!=null&¶mMap.size()>0)
返回JSONObject.toJSONString(paramMap);
其他的
返回“”;
}
如果您想要将Hashmap放入JSONObject中,为什么要使用它

编辑:根据

EDIT2:在使用的JSONObject方法上,我遵循以下代码:,该方法未被弃用

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

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

这对我很有用。

我已将你的两个帐户合并在一起。另外,StackOverflow不是一个论坛;如果你有新问题,请提出新问题。如果您想在问题中包含更多信息,请。如果你想与回答问题的人进行互动,你可以给他们留下评论。没有
add(jsonObj)
,但是
put(jsonObj)
是可能的(似乎也没有必要转换为JSONString)@someone某处:有add方法:方法toJSONString()类型JSONObject未定义??似乎将编辑更改为
valueToString()
的标准。jsonArray对象中没有push()方法。请尝试解释您认为此代码可以解决问题的原因。我认为这无法回答问题。这与问题完全无关。
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());
JSONArray successObject=new JSONArray();
JSONObject dataObject=new JSONObject();
successObject.put(dataObject.toString());
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());
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"}]