我想将JSONObject添加到JSONArray并将该JSONArray包含在其他JSONObject中 [英] I want to add a JSONObject to a JSONArray and that JSONArray included in other JSONObject

查看:129
本文介绍了我想将JSONObject添加到JSONArray并将该JSONArray包含在其他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 herer 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);

如果想要将Hashmap放入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆