简体   繁体   中英

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.

Thanks @Sufian

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

This does solve my problem

You should change your json. Your json data's id parameters doesn't have " character; Control your id paramerters.

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

The json provided by you is wrong. I have solved you error in your json string

You can get this right json string from : http://pastie.org/private/1xbyzgamzihswnpgtz15yw

You can verify this json string using this url : http://jsonviewer.stack.hu/

I hope it was helpfull.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM