【问题标题】:Error in parsing JSON in Android在 Android 中解析 JSON 时出错
【发布时间】:2012-06-11 13:10:08
【问题描述】:

我正在从网站上抓取数据。我们得到的数据格式为

[
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "7:26pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "7:46pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:00pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "9:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:20pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "9:24pm",
        "arrStation": "Kogarah Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:27pm",
        "depStation": "Kogarah Station ",
        "depPlatform": "4",
        "arrTime": "9:31pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "4"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "7:26pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "7:46pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:00pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "9:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "9:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:16pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "8:38pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:54pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "10:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "10:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "10:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "9:23pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "9:45pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "9:54pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "11:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "11:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "11:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
{
    "1": {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "5:09am+",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "5:11am+",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
}

]

当我将上述数据转换为 Json 格式时,出现以下错误

    06-11 18:36:40.584: V/Error(657): Error in parsing Innerorg.json.JSONException: Value {"1":{"depTime":"5:09am+","arrStation":"Allawah Station ","arrTime":"5:11am+","train":"Eastern Suburbs and Illawarra train (CityRail) ","arrPlatform":"3","depStation":"Hurstville Station ","depPlatform":"3"}} at 4 of type org.json.JSONObject cannot be converted to JSONArray

如何在 Android 中解析这种类型的 json 字符串。有什么建议的朋友......?

【问题讨论】:

  • 你上面的json字符串无效粘贴here and see
  • 你的json格式不对,可以在jsonlint.com验证
  • json格式无效,可以到jsonlint.com查看json格式
  • 我刚刚输入了我的json部分的格式。现在我已经更新了完整的json字符串。

标签: android json


【解决方案1】:

那是因为你的 JSON 字符串构造不好。

试着把它放进去:http://jsonlint.com/它会告诉你当前的错误。

另外,请在http://json.org/ 阅读一些关于 JSON 的文档,因为您实际上犯了一些基本错误:)

如果您还提供您的 JSON 必须表示的内容,我可以显示您的 JSON 字符串的正确形式 :)

【讨论】:

  • 对。此 Json 包含 2 个无效的根元素。
【解决方案2】:

我的字符串如下所示

{
"result": "success",
"countryCodeList": [
{
"countryName": "World Wide",
"countryCode": "00"
},
{
"countryName": "Korea, Republic of",
"countryCode": "kr"
},
{
"countryName": "United States",
"countryCode": "us"
},-
{
"countryName": "Japan",
"countryCode": "jp"
},
{
"countryName": "China",
"countryCode": "cn"
},
{
"countryName": "India",
"countryCode": "in"
}
]
}

我已经完成了

JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

    valArray1.toString().replace("[", "");
            valArray1.toString().replace("]", "");

            int len = valArray1.length();

            for (int i = 0; i < valArray1.length(); i++) {

                Country country = new Country();
                JSONObject arr = valArray1.getJSONObject(i);

                country.setCountryCode(arr.getString("countryCode"));
                country.setCountryName(arr.getString("countryName"));
                arrCountries.add(country);
            }

【讨论】:

  • 我刚刚输入了部分字符串。实际上它是一种有效的 json 格式。我已签入jsonlint.org。如果你愿意,现在可以检查我的字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
相关资源
最近更新 更多