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
Android中的复杂Json解析_Android_Json_Parsing - Fatal编程技术网

Android中的复杂Json解析

Android中的复杂Json解析,android,json,parsing,Android,Json,Parsing,在我的应用程序中,我想解析json响应,其格式为 {"quote":[{"orderId":"3209926"},{"totalpages":1}]} 下面是我所做的代码,但问题是如何获得“totalpages”值 有人知道如何解析它吗?提前谢谢请注意,与其他的get函数一样,如果对象不包含请求的键,则返回throw。因此,在请求密钥之前,应该使用它来确定对象是否包含密钥 例如,在For循环中,可以执行以下操作: JSONObject offerObject = jArray.getJSON

在我的应用程序中,我想解析json响应,其格式为

{"quote":[{"orderId":"3209926"},{"totalpages":1}]} 
下面是我所做的代码,但问题是如何获得“totalpages”值

有人知道如何解析它吗?提前谢谢

请注意,与其他的get函数一样,如果对象不包含请求的键,则返回throw。因此,在请求密钥之前,应该使用它来确定对象是否包含密钥

例如,在For循环中,可以执行以下操作:

JSONObject offerObject = jArray.getJSONObject(i);
if(offerObject.has("orderId") {
    current.orderId = offerObject.getInt("orderId");
}
if(offerObject.has("totalpages") {
    current.totalpage= offerObject.getInt("totalpages");
}
您还可以在循环后添加标志和检查,以确保orderId和totalpages都存在于JSON数据中。

请注意,与throw的其他get函数一样,如果对象不包含请求的键,则返回。因此,在请求密钥之前,应该使用它来确定对象是否包含密钥

例如,在For循环中,可以执行以下操作:

JSONObject offerObject = jArray.getJSONObject(i);
if(offerObject.has("orderId") {
    current.orderId = offerObject.getInt("orderId");
}
if(offerObject.has("totalpages") {
    current.totalpage= offerObject.getInt("totalpages");
}

您还可以在循环后添加一个标志和一个检查,以确保orderId和totalpages都出现在JSON数据中。

我不知道您的JSON为什么会有这种结构。但是如果您想解析它,那么您必须对函数执行如下操作

for (int i = 0; i < jArray.length(); i++) {
        JSONObject offerObject = jArray.getJSONObject(i);
        if(offerObject.has("orderId")) {
          current.orderId = offerObject.getInt("orderId");
        } else if(offerObject.has("totalpages")) {
          current.totalpage= offerObject.getInt("totalpages");
        }
}
for(int i=0;i
我不知道你的json为什么会有这样的结构。但是如果您想解析它,那么您必须对函数执行如下操作

for (int i = 0; i < jArray.length(); i++) {
        JSONObject offerObject = jArray.getJSONObject(i);
        if(offerObject.has("orderId")) {
          current.orderId = offerObject.getInt("orderId");
        } else if(offerObject.has("totalpages")) {
          current.totalpage= offerObject.getInt("totalpages");
        }
}
for(int i=0;i
show your full implementation show your full implementation实际上响应包含orderid和totalpages的值。是的,但是在循环的第一次迭代中检查的子对象不包含“totalpages”。类似地,在第二次迭代中检查的子对象不包含“orderId”。在第一次迭代中检查的子对象是:
{“orderId”:“3209926”}
。第二个检查的子对象是:
{“totalpages”:1}
。在循环遍历“quote”数组时,首先检查属性是否存在,如果存在,然后请求其值,否则继续检查其他属性。请参阅上面的代码。实际上响应包含orderid和totalpages的值。是的,但是在循环的第一次迭代中检查的子对象不包含“totalpages”。类似地,在第二次迭代中检查的子对象不包含“orderId”。在第一次迭代中检查的子对象是:
{“orderId”:“3209926”}
。第二个检查的子对象是:
{“totalpages”:1}
。在循环遍历“quote”数组时,首先检查属性是否存在,如果存在,然后请求其值,否则继续检查其他属性。请参阅上面的代码。我尝试过了,但logcat中显示的响应是错误解析数据org.json.JSONException:totalpagescan没有值您检查offerObject.has的结果(“totalpages”)还有一件事你可以使用这个类吗?我试过了,但是logcat中显示的响应是错误解析数据org.json.JSONException:totalpagescan没有值。你可以检查offerObject.has(“totalpages”)的结果。还有一件事你可以使用这个类吗