Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse nested json array in android using volley library

I have nested json array in below format.I am using volley liabrary for JSON Parsing.

{
"City": [{
        "name": "Mumbai",
        "Mumbai": [{
            "area": "andheri",

            "diler": [{
                "DName": "yuvraj"
            }]
        }, {
            "area": "jogeshwari"
        }, {
            "area": "goregaon"
        }]
    },

    {

        "name": "Nashik",
        "Nashik": [{
            "area": "clg rd",
            "diler": [{
                "DName": "yuvraj"
            }]
        }, {
            "area": "GP RD",
            "diler": [{
                "DName": "Roshan"
            }]
        }, {
            "area": "CBS",
            "diler": [{
                "DName": "Deepak"
            }]
        }]

    }, {
        "name": "Bengaluru"
    }
]}

Below is the code which i have write in android.

   jsonURL = "http://192.168.1.11/cycle_webservices/testing.json";

    buttonReq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                    jsonURL,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            try {
                                for (int i = 0; i < response.length(); i++) {
                                    JSONObject jsonObject = response.getJSONObject(i);
                                    String name = jsonObject.getString("name");
                                    String area = jsonObject.getString("area");
                                    String diler = jsonObject.getString("diler");

                                    textView.append("\nCity: " + name + "\nArea: " + area + "\nDealer: " + diler +   "\n");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("VOLLEY", "ERROR");
                            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
            );
            requestQueue.add(jsonArrayRequest);
        }
    });

But i am getting Errors while parsing it in Android. The error is- JSONObject can not be converted to JSONArray

Can anyone please provide sample code to parse this json Array.

Thanks in Advance

like image 572
Monali Avatar asked Nov 26 '16 12:11

Monali


3 Answers

Here is your problem solution, use below code

 try {
        JSONObject jsonObject = new JSONObject("response");
        JSONArray jsonArray = jsonObject.getJSONArray("City");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String name = object.getString("name");
            if (object.length() != 0) {
                Iterator<String> key = object.keys();
                while (key.hasNext()) {
                    String cityname = key.next();
                    JSONArray ja = object.getJSONArray(cityname);
                    for (int j = 0; j < ja.length(); j++) {
                        JSONObject object1 = ja.getJSONObject(j);
                        String area = object1.getString("area");

                    }
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

And try to make your JSON format same.

like image 84
Sanwal Singh Avatar answered Nov 01 '22 02:11

Sanwal Singh


The point is the first json node is a JsonObject, change your code this way and continue parsing:

new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) { 
like image 41
Mohsen Mirhoseini Avatar answered Nov 01 '22 01:11

Mohsen Mirhoseini


Use JSONObject for parsing data like:

  JSONObject jsonObject = new JSONObject(response);

  JSONArray jsonArray = jsonObject.getJSONArray("City");

  for(int i = 0; i<jsonArray.length(); i++)
  {
      JSONObject jsonObject = jsonArray .getJSONObject(i);

      String name = jsonObject.getString("name");

      JSONArray city_array = jsonObject.getJSONArray(name);

      for(int j=0; j<city_array.length(); j++)
      {
          JSONObject obj = city_array.getJSONObject(j);

          String area = obj.getString("area");

          JSONArray diler_array = obj.getJSONArray("diler");

          JSONObject obj1 = diler_array.getJSONObject(0);

          String DName = obj1.getString("DName");
      }
  }

Please check edited answer.

like image 30
Mahesh Gawhane Avatar answered Nov 01 '22 02:11

Mahesh Gawhane