我想将 JSONObject 添加到 JSONArray 并且该 JSONArray 包含在其他 JSONObject 中 [英] I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject
问题描述
我需要以下在java中构造的结构并将其作为响应发送:
I need below kind of structure constructed in java and send it as response :
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 字符串,如:
and here above function is converting paramMap into json string like:
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?
这是我得到的输出:
{"action":"Remove","datatable":[{\"userid\":\"userid0\",\"srcOfPhoto\":\"users\\\/JMMBGTCHG.jpg\",\"username\":\"name0\"}"],"msgType":"success"}
没有在 javascript 中解析.
which is not being parsed in 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);
及以上函数是将 paramMap 转换为 json 字符串:
and above function is converting paramMap into json string:
public static String getJSONMessage(Map<String, String> paramMap){
if(paramMap != null && paramMap.size() > 0)
return JSONObject.toJSONString(paramMap);
else
return "";
}
推荐答案
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);
如果您想要将它放入 JSONObject,为什么要使用 Hashmap?
Why were you using an Hashmap if what you wanted was to put it into a JSONObject?
根据 http://www.json.org/javadoc/org/json/JSONArray.html
在使用的 JSONObject 方法上,我正在关注以下可用代码:https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327,该方法未被弃用.
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.
我们存储的是 JSONObject 的字符串表示,而不是 JSONObject 本身
We're storing a string representation of the JSONObject, not the JSONObject itself
这篇关于我想将 JSONObject 添加到 JSONArray 并且该 JSONArray 包含在其他 JSONObject 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!