1
{"Suggestions":[{"Itinerary_1":[{"Cities":"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai","Citieids":"14-85-114-2","Kms":595}],"Itinerary_2":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}],"Itinerary_3":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}]}]}

This my json response.This json have a inner array how can i get the value

This my code:

class itinerarydetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;


            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("orgin", "14"));
            params.add(new BasicNameValuePair("destination", "2"));
            params.add(new BasicNameValuePair("orgin_date", "28/06/2017"));
            params.add(new BasicNameValuePair("destination_date", "07/07/2017"));

             json = jsonParser.makeHttpRequest(itineraryurl, "GET",
                    params);
            try {

                //Suggestions
                jsonarray = json.getJSONArray("Suggestions");
               // JSONArray list = mainObj.getJSONArray("prodCat_list");

                String check;
                if(jsonarray != null) {
                   // Toast.makeText(getApplicationCon                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          text(), "MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE",
                    //  Toast.LENGTH_SHORT).show();

                    Log.d("check","MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE");

                    for (int i = 0; i < jsonarray.length(); i++) {
                        //   JSONObject elem = list.getJSONObject(i);

                        JSONObject object = jsonarray.getJSONObject(i);

                        //JSONArray prods = elem.getJSONArray("prods");

                        jsoninsidearray = object.getJSONArray("Itinerary_1");

                        if(jsoninsidearray != null) {

                            Log.d("check","INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE");

                            for (int j = 0; j < jsoninsidearray.length(); j++) {
                                JSONObject innerElem = jsoninsidearray.getJSONObject(j);


                                if(innerElem != null) {


                                    Log.d("check","innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                                    cityjson = innerElem.getString("Cities");
                                    itinerary = innerElem.getString("Citieids");
                                    totalkms = innerElem.getString("Kms");

                                }
                               else
                                {
                                    Log.d("check","innerElement ARRAY IS EMPTY");

                                }

                            }
                        }
                        else
                        {
                           Toast.makeText(getApplicationContext(), "INSIDE ARRAY IS EMPTY",
                                   Toast.LENGTH_SHORT).show();

                            Log.d("check","INSIDE ARRAY IS EMPTY");
                        }


                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "MAIN ARRAY IS EMPTY",
                            Toast.LENGTH_SHORT).show();

                    Log.d("check","MAIN ARRAY IS EMPTY");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }


        protected void onPostExecute(String file_url) {

            Toast.makeText(getApplicationContext(), "hai",
                    Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), cityjson, Toast.LENGTH_SHORT).show();



        }

    }

I am Vignesh i have a doubt so pls help in this below but empty values has displayed.how can i get the values from inner array from json.

4
  • I think this answers your question: stackoverflow.com/questions/17673057/… Commented Jul 4, 2017 at 5:07
  • In this,there is the Suggestion Array in that there are three different Array("Itinerary_1","Itinerary_2","Itinerary_3") .but the answer given by You has (prodCat_list) array and it has a Multiple array but everything Has same name so it could be called easily.I have also tried out this idea but it displays the empty value. Commented Jul 4, 2017 at 5:14
  • Possible duplicate of How to parse this nested JSON array in android Commented Jul 4, 2017 at 6:00
  • object.getJSONArray("Itinerary_"+ (i+1)) is perfect because there is no "Itinerary_0". Than it i will stop parsing dear. Check my answer Commented Jul 4, 2017 at 7:11

5 Answers 5

3

Let's try to understand the structure of your input JSON object.

  • Suggestions (JSONArray)
    • First Object (JSONObject)
      • Itinerary_1 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
      • Itinerary_2 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
      • Itinerary_3 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
    • Second Object (JSONObject)
      • Itinerary_1 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
      • Itinerary_2 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
      • Itinerary_3 (JSONArray)
        • First Object (JSONObject)
          • cities (String)
          • citieids (String)
          • kms (String)
    • so on ....

formatted JSON Object

{
   "Suggestions":[
      {
         "Itinerary_1":[
            {
               "Cities":"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai",
               "Citieids":"14-85-114-2",
               "Kms":595
            }
         ],
         "Itinerary_2":[
            {
               "Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai",
               "Citieids":"14-114-114-2",
               "Kms":560
            }
         ],
         "Itinerary_3":[
            {
               "Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai",
               "Citieids":"14-114-114-2",
               "Kms":560
            }
         ]
      }
   ]
}

Hence your code should have an additional for loop to loop over 3 itinerary elements of each JSONObject inside Suggestions.

Here is the updated code, check out the new for loop added with index as the iteration counter.

    try {
        JSONObject json = new JSONObject("{\"Suggestions\":[{\"Itinerary_1\":[{\"Cities\":\"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai\",\"Citieids\":\"14-85-114-2\",\"Kms\":595}],\"Itinerary_2\":[{\"Cities\":\"Madurai - Pillayarpatti - Pillayarpatti - Chennai\",\"Citieids\":\"14-114-114-2\",\"Kms\":560}],\"Itinerary_3\":[{\"Cities\":\"Madurai - Pillayarpatti - Pillayarpatti - Chennai\",\"Citieids\":\"14-114-114-2\",\"Kms\":560}]}]}");
        JSONArray jsonarray = json.getJSONArray("Suggestions");
        if(jsonarray != null) {
            Log.d("check","MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE");
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject object = jsonarray.getJSONObject(i);
                // ** START NEW FOR LOOP
                for (int index=1; index < 4; index++) {
                    Log.d("check","-------------------------");
                    Log.d("check","Print Itinerary_" + (index));
                    JSONArray jsoninsidearray = object.getJSONArray("Itinerary_" + index);
                    if(jsoninsidearray != null) {
                        Log.d("check","INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                        for (int j = 0; j < jsoninsidearray.length(); j++) {
                            JSONObject innerElem = jsoninsidearray.getJSONObject(j);
                            if(innerElem != null) {
                                Log.d("check","innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                                String cityjson = innerElem.getString("Cities");
                                String itinerary = innerElem.getString("Citieids");
                                String totalkms = innerElem.getString("Kms");
                                Log.d("check", cityjson + " | " + itinerary + " | " + totalkms);
                            } else {
                                Log.d("check","innerElement ARRAY IS EMPTY");
                            }
                        }
                    } else {
                        Log.d("check","INSIDE ARRAY IS EMPTY");
                    }
                } // ** END NEW FOR LOOP
            Log.d("check", "");
            }
        } else {
            Log.d("check","MAIN ARRAY IS EMPTY");
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Sample Run

07-03 22:03:05.028 30616-30616/a D/check: MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: --------------------------
07-03 22:03:05.028 30616-30616/a D/check: Print Itinerary_1
07-03 22:03:05.028 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai | 14-85-114-2 | 595
07-03 22:03:05.028 30616-30616/a D/check: --------------------------
07-03 22:03:05.028 30616-30616/a D/check: Print Itinerary_2
07-03 22:03:05.028 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: Madurai - Pillayarpatti - Pillayarpatti - Chennai | 14-114-114-2 | 560
07-03 22:03:05.029 30616-30616/a D/check: --------------------------
07-03 22:03:05.029 30616-30616/a D/check: Print Itinerary_3
07-03 22:03:05.029 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: Madurai - Pillayarpatti - Pillayarpatti - Chennai | 14-114-114-2 | 560
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution.This code perfectly works sir. but only output displayed in static data. not retrieve the data from from json(Dynamically). I think the problem is JSON ARRAY not converted into JSON OBJECT
I didn't get your comments, I can help if you elaborate?
0

Just Change this line jsoninsidearray = object.getJSONArray("Itinerary_1");

to

jsoninsidearray = object.getJSONArray("Itinerary_"+i);

7 Comments

Ok Try this and let me know if you have any query
Thanks for Your Response. I tried it out but only empty value is displayed.
Just for my future understanding, how is the 2nd code different from the first?
for (int i = 0; i < jsonarray.length(); i++) { JSONObject object = jsonarray.getJSONObject(i); jsoninsidearray = object.getJSONArray("Itinerary_1"); // Instead of Itinerary_1,Itinerary_2,Itinerary_3 jsoninsidearray = object.getJSONArray("Itinerary_"+i); // } In the above code instead of using Instead of using "Itinerary_1,Itinerary_2,Itinerary_3" like this We could use [Itinerary_"+i] like this which could call entirely.
any doubt please ask me.
|
0

here you have to create jsonobject first try this

JSONObject object = new JSONObject(json);  
JSONArray data = object.getJSONArray("Suggestions");   

        JSONObject obj1 = data.getJSONObject(0);

            JSONArray inner = obj1.getJSONArray("Itinerary_1");
            String Cities=inner.getString(0);
            String Citiesids=inner.getString(1);
            String Kms=inner.getString(2);
//and so on copy and change the name of Itinerary_*

}

Comments

0

May this will help you

JSONObject mainObj = = new JSONObject(response);

JSONArray Suggestionslist = mainObj.getJSONArray("Suggestions");
    if(Suggestionslist != null){

    for(int i = 0; i < Suggestionslist.length();i++){
             JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                 JSONArray Itinerary = elem.getJSONArray("Itinerary_"+ (i+1));
                if(Itinerary != null){ 

                       for(int j = 0; j < Itinerary.length();j++){
                        JSONObject innerElem = Itinerary.getJSONObject(j);
                        if(innerElem != null){
                            String Cities = innerELem.getString("Cities");
                            String Citieids = innerElem.getString("Citieids");
                            String Kms = innerElem.getString("Kms");
                        }
                    }  

                 }
    }

}

3 Comments

If any problem occure please mention
What is result? "Itinerary_"+ (i+1) is perfect because there is no "Itinerary_0". Than it i will stop parsing dear.
Thanks for Your Response. I tried it out but only empty value is displayed
0

This is Complete Solution for innerArray

      if (status == 200) {
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);



            JSONObject jsono = new JSONObject(data);
            JSONArray jarray = jsono.getJSONArray("Suggestions");

            System.out.println("====================///////////" + jarray);
            if (jarray != null) {
                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);


                    System.out.print("obj//////////" + object.length() + "\n");
                    int m;


                    for (int i1 = 1; i1 <= object.length(); i1++) {

                        System.out.println("inner======  inside second for loop  =====");

                        JSONArray jarray1 = object.getJSONArray("Itinerary_" + i1);

                        JSONObject object1 = jarray1.getJSONObject(i);

                        //JSONObject jsoninside = object.getJSONObject("Itinerary_"+ i1);

                        System.out.println("inner====================///////////\\\\" + object1);

                        if (object1 != null) {
                            citynames = object1.getString("Cities");
                            citykms = object1.getString("Kms");

                        } else {
                            Log.d("check", "INSIDE ARRAY IS EMPTY");
                        }


                    }

                    return true;
                }

            }
        } else {
            Log.d("check", "MAIN ARRAY IS EMPTY");
        }

1 Comment

This code Working Perfectly (any queries please add comment).I will Explain

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.