无法解析此json响应

时间:2011-05-24 01:23:25

标签: android json parsing

我正在尝试解析以下json响应:

[
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6203,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":16000,
                    "description":"5-Door",
                    "modelYear":2011,
                    "name":"Scion xb",
                    "curbWeight":0,
                    "@id":"81454531",
                    "effectiveDate":"4\/28\/10",
                    "displayName":"xB",
                    "deliveryFee":775
                },
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6202,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":16950,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xb",
                    "curbWeight":0,
                    "@id":"81454516",
                    "effectiveDate":"4\/28\/10",
                    "displayName":"xB",
                    "deliveryFee":720
                }
            ]
        },
        "name":"xB",
        "@id":"Scion xb"
    },
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"3 DOOR LIFTBACK",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6223,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":18275,
                    "description":"2 DOOR L\/B",
                    "modelYear":2011,
                    "name":"Scion tC",
                    "curbWeight":3945,
                    "@id":"84604049",
                    "effectiveDate":"8\/6\/10",
                    "displayName":"tC",
                    "deliveryFee":720
                },
                {
                    "bodyStyle":"3 DOOR LIFTBACK",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6222,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":19275,
                    "description":"2 DOOR L\/B",
                    "modelYear":2011,
                    "name":"Scion tC",
                    "curbWeight":3945,
                    "@id":"84604028",
                    "effectiveDate":"8\/6\/10",
                    "displayName":"tC",
                    "deliveryFee":720
                }
            ]
        },
        "name":"tC",
        "@id":"Scion tC"
    },
    {
        "modelTrimList":{
            "modelTrim":[
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6233,
                    "transmission":"Manual",
                    "cityMpg":0,
                    "msrp":15045,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xd",
                    "curbWeight":3605,
                    "@id":"91724869",
                    "effectiveDate":"12\/8\/10",
                    "displayName":"xD",
                    "deliveryFee":775
                },
                {
                    "bodyStyle":"5-DOOR",
                    "cylinders":4,
                    "highwayMpg":0,
                    "modelCode":6232,
                    "transmission":"Automatic",
                    "cityMpg":0,
                    "msrp":15845,
                    "description":"5-DOOR",
                    "modelYear":2011,
                    "name":"Scion xd",
                    "curbWeight":3605,
                    "@id":"91724562",
                    "effectiveDate":"12\/8\/10",
                    "displayName":"xD",
                    "deliveryFee":775
                }
            ]
        },
        "name":"xD",
        "@id":"Scion xd"
    }
]

我似乎无法正确解析它,请提供建议,如果可能的话,我可以使用示例代码,任何想法都非常受欢迎。

3 个答案:

答案 0 :(得分:2)

你需要用[]包装它:现在它是无效的JSON。

编辑:如果它被正确包裹,请尝试:

JSONArray js = new JSONArray(newjson);
        Toast.makeText(getApplicationContext(),js.toString(), Toast.LENGTH_LONG).show();
        for (int i = 0; i < js.length(); i++) {
            Toast.makeText(getApplicationContext(),js.getJSONObject(i).getString("name").toString(), Toast.LENGTH_LONG)
            .show();

答案 1 :(得分:0)

那是因为您的JSON无效,看起来它应该是modelTrimList的数组

答案 2 :(得分:0)

最简单的方法是通过在线验证工具(如JSON Lint:

)运行它

http://jsonlint.com/

根据您的示例,它建议您在第42行出错:

第42行的解析错误: ......“@id”:“Scion xb”},{“modelTrimLi ---------------------- ^ 期待'EOF'