Getting JSONException
I have a json object:
"images":{"1":{"imagename":"image1.gif","url":"image1url"},"2":{"imagename":"image2.gif","url":"image2url"},"3":{"imagename":"image3.gif","url":"image3url"}}
I want to fetch imagename and url from this. I enter into this images. I fetched the values 1,2,3 from images. But I am not able to fetch the json corresponding to this 1,2 and 3.
Its throwing exception stating: No value for 1 or No Value for 2 or No value for 3
What may be the reason for this cause? Please reply..
My present code is:
if(jsonObj.has("images")) {
JSONArray imagesArray = jsonObj.getJSONOb开发者_开发技巧ject("images").names();
JSONObject imageDetailsObject;
for(int i = 0; i < imagesArray.length(); i++) {
imageDetailsObject = jsonObj.getJSONObject(imagesArray.get(i).toString());
if(imageDetailsObject.has("imagename")) {
//perform some actions
}
if(imageDetailsObject.has("url")) {
//perform some actions
}
}
}
EDITED:
imageDetailsObject = jsonObj.getJSONObject("images").getJSONObject(imagesArray.get(i).toString());
I got it worked by giving:
if (jsonObj.has("images")) {
JSONArray imagesArray = jsonObj.getJSONObject("images").names();
JSONObject imageDetailsObject;
for (int i = 0; i < imagesArray.length(); i++) {
imageDetailsObject = jsonObj.getJSONObject("images").getJSONObject(
imagesArray.getString(i));
if(imageDetailsObject.has("imagename")) {
//perform some actions
}
if (imageDetailsObject.has("url")) {
//perform some actions
}
}
}
精彩评论