【问题标题】:Trouble parsing this json response解析此 json 响应时遇到问题
【发布时间】:2011-05-24 01:23:24
【问题描述】:

我正在尝试解析以下 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"
    }
]

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

【问题讨论】:

  • jsonlint.com - 除非您再次编辑,否则您的帖子是有效的 JSON。确保您向我们展示的内容就是您正在解析的内容:这应该是复制/粘贴,而不是“错字”。

标签: android json parsing


【解决方案1】:

你需要用 [] 包裹它:现在它不是有效的 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();

【讨论】:

  • 同意,您的 JSON 中的某些内容看起来不对。
  • 对不起,它被包裹在 [] 中,这只是一个错字。
  • 嗯,我以为是解析,只返回数组的一个成员?
  • 现在我有正确的 json 格式,它甚至没有让我成为数组的第一个成员。所以我编辑了这个问题,并发布了要解析的 json 响应。感谢您抽出时间和帮助。
  • 更新您的帖子并包含您正在使用的实际 Java 代码。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    回答这个问题的最简单方法是通过 JSON Lint 等在线验证工具运行它:

    http://jsonlint.com/

    您的示例表明您在第 42 行有错误:

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

    【讨论】:

    • 修复了回复谢谢,但问题仍然存在,我似乎无法正确解析。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多