Json array inside array retrieve Android values
im trying to get the values from JSONArray
inside the array, i can get all the JSON values in JSONArray
successfully but not able to get the values inside JSONArray
. When I convert JSONArray
to JSONObject
to get the values stored internally JSONArray
. It gives an error:org.json.JSONException: No value for "banner"
Here is the JSON code, I checked the JSON code from jsonlint.com and it showed that JSON is Validate,
[
{"code":"banner","moduletitle":0,
"banner":
[
{"image":"http://imageurl"},
{"image":"http://imageurl"},
{"image":"http://imageurl"}
]
}
]
I am trying to get this from 3 hours away but no luck. I'm new to JSON and don't know how JSON actually work, and also read abit GSON library to get JSON values. here is my java code.
JSONArray jsonObj = null;
String image_url = "";
String banner_code ="";
try {
jsonObj =new JSONArray(lib_function.getJSONUrl( jsontags.Top_Banner_JOSN_URLs));
Log.d("value retrun :","" +jsonObj);
//---vlaue is coming and print in Log ----//
} catch (JSONException e) {
Log.v("Error in Parser :", " " + e);
Log.d("no value retrun :", "failed to convert");
}
try{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
JSONArray subArray = jo.getJSONArray("banner");
image_url= subArray.getString(Integer.parseInt("image"));
Log.d("banner code",""+subArray);
}catch(Exception e)
{
Log.d("not working",""+e);
}
I'm considering this question, but good luck: How to parse a JSON array inside another JSON array in Android
If anyone can suggest what I am doing wrong, I would appreciate it. or let me know where can I get more information about json
UPDATE thanks everyone too much to give them precious time to answer my stupid question. All answers are correct, but I can only accept one answer. Thank you all very much
source to share
Here:
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
Since it is parsing jsonObj
JSONArray
, so there is no need to create new ones JSONArray
and JSONObject
to extract it from jsonObj
. delete all above three lines.
banner
JSONArray
is inside JSONObject
, which is contained in jsonObj
JSONArray
, gets it like:
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 the jsonObj
JSONArray contains multiple JSONObjects, then use for-loop
it to iterate over it.
source to share
If you need the value "image" which is in the json url than
String response = "your response";
try{
JsonArray jAry = new JsonArray(response);
JsonObject jObj = jAry.getJsonObject(0);
JsonArray jsonBanner = jObj.getJsonArray("banner");
JsonObject temp;
for(int i=0;i<jsonBanner.length;i++){
temp = jsonBanner.getJsonObject(i);
String image = temp.optString("image");
}
}
source to share