-1

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"}
		]
		
	}
]

I m trying to get this from 3 hour but no luck. i m new in JSON and do not know how JSON Actually work, and also read abut 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 folllow this question but luck: How to parse JSON Array inside another JSON Array in Android

If anyone suggest, what i m doing wrong will be appreciate. or let me know, where i can get more information about json

UPDATE thanks too all to give their precious time for answering my stupid question. All answers are correct , but i can accept only one answer. A Big thanks to all

1
  • 1
    I the JSONArray create another JSONObject and get the value with specifying key. Commented Jan 28, 2016 at 11:43

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

jsonObj is such a misleading name. This should be renamed to jsonArray for readability reasons :) +1 for your concise answer
what about code and image inside banner how can i get values of theme ?@ρяσѕρєя K
1

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");

Comments

1
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

Comments

1

If You want value of "image" which is in json arrray 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");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.