Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i m trying to get values from JSONArray inside array, i m able to retrieve whole JSON values into JSONArray successfully but not able to retrieve values inside JSONArray. When i convert JSONArray to JSONObject to get values stored inside JSONArray. It gives error: org.json.JSONException: No value for "banner"

Here is JSON code, i verified JSON code with jsonlint.com and it showed JSON is Validate,

[
	{"code":"banner","moduletitle":0,
	  "banner":
		[
			{"image":"http://imageurl"},
			{"image":"http://imageurl"},
			{"image":"http://imageurl"}
		]
		
	}
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here:

JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);

Because parsing jsonObj JSONArray so no need to create new JSONArray and JSONObject to extract it from jsonObj. remove all above three lines.

banner JSONArray is inside JSONObject which is contained by jsonObj JSONArray, get it as:

   JSONObject jsonObject=jsonObj.optJSONObject(0);
    JSONArray subArray = jsonObject.getJSONArray("banner");

   // get code key from `jsonObject`
   String strCode=jsonObject.optString("code");

   // get all images urls from `subArray`
    for(int index=0;index<subArray.length();index++){
      JSONObject imgJSONObject=subArray.optJSONObject(index);
      // get image urls
      String strImgURL=imgJSONObject.optString("image");

     } 

Also, if jsonObj JSONArray contains multiple JSONObject's then use for-loop to iterate it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...