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 c
Solution 1:
Here:
JSONObject jo = newJSONObject();
JSONArray ja = newJSONArray();
// 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 urlsString strImgURL=imgJSONObject.optString("image");
}
Also, if jsonObj
JSONArray contains multiple JSONObject's then use for-loop
to iterate it.
Solution 2:
I am assuming you have the rest of the values accessible to you, so posting just this snippet.
code=jsonObject.getString("code");
moduletitle=jsonObject.getString("moduletitle");
banner=jsonObject.getJSONArray("banner");
Solution 3:
jsonObj =new JSONArray(lib_function.getJSONUrl( jsontags.Top_Banner_JOSN_URLs);
From above line you will get JSONArray. So now loop it and get you banner JSONArray.Again loop bannerArray and you will get image Urls
Solution 4:
If You want value of "image" which is in json arrray than
Stringresponse="your response";
try{
JsonArrayjAry=newJsonArray(response);
JsonObjectjObj= jAry.getJsonObject(0);
JsonArrayjsonBanner= jObj.getJsonArray("banner");
JsonObject temp;
for(int i=0;i<jsonBanner.length;i++){
temp = jsonBanner.getJsonObject(i);
Stringimage= temp.optString("image");
}
}
Post a Comment for "Json Array Inside Array Retrieve Values Android"