Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.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
Java 如何在JSON数组中获取JSON数组?_Java_Android_Arrays_Json_Chat - Fatal编程技术网

Java 如何在JSON数组中获取JSON数组?

Java 如何在JSON数组中获取JSON数组?,java,android,arrays,json,chat,Java,Android,Arrays,Json,Chat,我有来自url(服务器)的消息的JSON响应 { “答复”:[ 79457, { “中”:321, “日期”:123, "出局":一,, “uid”:984, “读取状态”:1, “标题”:“…”, “正文”:“消息”, “fwd_消息”:[ { “uid”:981, “日期”:152, “正文”:“转发消息1”, “fwd_消息”:[ { “uid”:654, “日期”:156, “正文”:“1” }, { “uid”:174, “日期”:158, “正文”:“2” } ] }, { “ui

我有来自url(服务器)的消息的JSON响应

{
“答复”:[
79457,
{
“中”:321,
“日期”:123,
"出局":一,,
“uid”:984,
“读取状态”:1,
“标题”:“…”,
“正文”:“消息”,
“fwd_消息”:[
{
“uid”:981,
“日期”:152,
“正文”:“转发消息1”,
“fwd_消息”:[
{
“uid”:654,
“日期”:156,
“正文”:“1”
},
{
“uid”:174,
“日期”:158,
“正文”:“2”
}
]
},
{
“uid”:949,
“日期”:651,
“正文”:“转发消息2”,
“fwd_消息”:[
{
“uid”:694,
“日期”:320,
“正文”:“32”,
“fwd_消息”:[
{
“uid”:152,
“日期”:111,
“正文”:“1”,
“fwd_消息”:[
{
“uid”:151,
“日期”:112,
“正文”:“8”
}
]
},
{
“uid”:542,
“日期”:315,
“正文”:“2”,
“fwd_消息”:[
{
“uid”:129,
“日期”:514,
“正文”:“1”,
“fwd_消息”:[
{
“uid”:635,
“日期”:225,
“正文”:“8”
}
]
}
]
}
]
},
{
“uid”:123,
“日期”:650,
“正文”:“,
“fwd_消息”:[
{
“uid”:154,
“日期”:122,
“正文”:“1”
},
{
“uid”:547,
“日期”:510,
“body”:“2”
}
]
}
]
}
]
}
]

}
这可能会对您有所帮助。我没有在编辑器上测试这段代码,但您可以从这段代码中了解它是如何工作的

JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
    JSONArray list = mainObj.getJSONArray("prodCat_list");
    if(list != null){
        for(int i = 0; i < list.length();i++){
            JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                JSONArray prods = elem.getJSONArray("prods");
                if(prods != null){
                    for(int j = 0; j < prods.length();j++){
                        JSONObject innerElem = prods.getJSONObject(j);
                        if(innerElem != null){
                            int cat_id = innerELem.getInt("cat_id");
                            int pos = innerElem.getInt("position");
                            String sku = innerElem.getString("sku");
                        }
                    }
                }
            }
        }
    }
}
JSONObject mainObj=新的JSONObject(myString);
如果(mainObj!=null){
JSONArray list=mainObj.getJSONArray(“prodCat_list”);
如果(列表!=null){
对于(int i=0;i
如果您使用的是
Gson
,它可以从Maven目录中获得,您可以从刚刚得到的响应中初始化一个变量,如下所示:

Gson gson = new Gson();
Message m = gson.fromJson(response, Message.class);
        JSONObject obj = null;
        try {
            obj = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
从那里开始,一个get就足够了
还有另一种选择,需要做更多的工作,但也很好。它包括从服务器的JSON响应生成一个
JSONObject
,并调用一个get来访问该
JSONObject
,它看起来是这样的:

Gson gson = new Gson();
Message m = gson.fromJson(response, Message.class);
        JSONObject obj = null;
        try {
            obj = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
然后,您可以调用另一个get,但可以从
obj
调用,如下所示:

JSONObject response = obj.getJSONObject("response");
JSONArray<FwdMessages> array = response.getJSONArray("fwd_messages");
JSONObject response=obj.getJSONObject(“response”);
JSONArray数组=response.getJSONArray(“fwd_消息”);

从那以后,你只需要1次就可以得到你要寻找的对象或属性。

数组的大小无关紧要吗?“fwd_消息”数组可以包含1条或5条转发消息