Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - Json array inside array retrieve values Android

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...