In my app, i want to parse json response which is in the format
{"quote":[{"orderId":"3209926"},{"totalpages":1}]}
below is the code which i had done,But the problem is how to get the “totalpages” value?
try {
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("quote");
for (int i = 0; i < jArray.length(); i++)
{
JSONObject offerObject = jArray.getJSONObject(i);
current.orderId = offerObject.getInt("orderId");
It shows error when i use
current.totalpage= offerObject.getInt("totalpages");
Anybody knows how to parse this?THanks in advance
Note that
getInt(), like other get-functions ofJSONObjectthrowJSONExceptionif the object does not contain the key requested. Thus, before you request the key you should usehasKey()to determine whether the object contains the key.For example, inside the for loop you can do the following:
You can also add a flag and a check after the loop to ensure that both orderId and totalpages were present in the JSON data.