-4

我有以下 JSON

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

当我解析 JSON 时出现以下异常

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

这是我的解析代码:

        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();
            }

它说我必须提供 JSON Araay 名称。虽然我提供它,但仍然是例外。这里有什么问题。

4

3 回答 3

0

你提供的json是错误的。我已经解决了你的 json 字符串中的错误

您可以从以下网址获取正确的 json 字符串: http: //pastie.org/private/1xbyzgamzihswnpgtz15yw

您可以使用此 url 验证此 json 字符串:http: //jsonviewer.stack.hu/

我希望它有帮助。

于 2014-05-23T07:04:06.213 回答
0

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"}]}]}
于 2014-05-23T06:41:09.327 回答
0

谢谢@Sufian

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

这确实解决了我的问题

于 2014-05-23T06:38:35.017 回答