Search code examples
androidjsonjsonexception

JSON parsing error, android?


I have the following JSON

{"DistributorDealerList":
{[{"Id":2,
"Name":"Distributor1",
"Dealer":
[{"Id":"1",
"Name":"Dealer1"},
{"Id":"2","
Name":"Dealer2"}]},
{"Id":4,"Name":"Distributor2",
"Dealer":
[{"Id":"3",
"Name":"Dealer3"}]}]}

When I am parsing the JSON getting the below exception

   org.json.JSONException: Names must be strings, but [{"Name":"Distributor1",
  "Dealer":[{"Name":"Dealer1","Id":"1"},{"Name":"Dealer2","Id":"2"}],"Id":2},
  {"Name":"Distributor2","Dealer":[{"Name":"Dealer3","Id":"3"}],"Id":4}] is of 
type org.json.JSONArray at character 195 of {"DistributorDealerList":
{[{"Id":2,"Name":"Distributor1","Dealer":[{"Id":"1","Name":"Dealer1"},
{"Id":"2","Name":"Dealer2"}]},{"Id":4,"Name":"Distributor2","Dealer":
 [{"Id":"3","Name":"Dealer3"}]}]}

HERE is my parsing code:

        try {
                JSONObject jsonObj = new JSONObject(apiResult);

                // Getting JSON Array node
                JSONArray contacts = jsonObj
                        .getJSONArray("DistributorDealerList");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String distId = c.getString("Id");
                    String distName = c.getString("Name");

                    JSONObject phone = c.getJSONObject("Dealer");
                    String distDealerID = phone.getString("Id");
                    String distDealerName = phone.getString("Name");            

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

It says that I must supply the JSON Araay Name. Though I am supplying it but still the exception. What is wrong here.


Solution

  • Thanks @Sufian

    http://json.parser.online.fr/

    This does solve my problem