JSON异常:org.json.JSONException:未终止的数组

时间:2012-04-26 13:24:25

标签: java android json

我正在开发一个Android应用程序 - 它从服务器请求一些数据,服务器以JSON格式返回数据。除了API之外,一切正常。调试后,我发现了这个异常:

org.json.JSONException: Unterminated array at character 152 of

{
    "total": "2",
    "result": [
        {
            "id": "15",
            "ename": "Horror movies based on true stories",
            "vname": "Những phim kinh dị dựa trên chuyện có thật",
            "movies": "16"
        }{
            "id": "14",
            "ename": "IMDB Top 250",
            "vname": "250 bộ phim hay nhất mọi thời đại",
            "movies": "127"
        }{
            "id": "13",
            "ename": "10 good movies for women",
            "vname": "10 bộ phim hay dành cho phái đẹp",
            "movies": "10"
        }{
            "id": "12",
            "ename": "The 84th Annual Academy Awards",
            "vname": "Giải Oscars lần thứ 84 (2012)",
            "movies": "37"
        }{
            "id": "11",
            "ename": "Charlie Chaplin collection",
            "vname": "Tuyển tập hề Sác lô",
            "movies": "7"
        }{
            "id": "10",
            "ename": "Tuyển tập điệp viên 007",
            "vname": "007 collection",
            "movies": "23"
        }{
            "id": "9",
            "ename": "Donnie Yen movies collection",
            "vname": "Tuyển tập phim Chung Tử Đơn",
            "movies": "24"
        }{
            "id": "8",
            "ename": "Back to the Future trilogy",
            "vname": "Tuyển tập "Trởlạitươnglai"",
            "movies": "3"
        }{
            "id": "7",
            "ename": "Stieg Larssons Millennium trilogy",
            "vname": "Bộ tiểu thuyết Millenium của nhà văn Stieg Larsson",
            "movies": "3"
        }{
            "id": "6",
            "ename": "Chan Wook Parks vengeance trilogy",
            "vname": "Bộ ba phim Báo thù của đạo diễn Park Chan Wook",
            "movies": "3"
        }
    ]
}

我在互联网上搜索过,但没有运气。而且我也算第152位,但没有错!请帮帮我!

6 个答案:

答案 0 :(得分:7)

我看错了:

数组中的元素应使用逗号分隔:

{
    "total":"2",
    "result":  [
        {
            "id":"15",
            "ename":"Horror movies based on true stories",
            "vname":"Nh?ng phim kinh d? du?a tren chuye?n co? tha?t",
            "movies":"16"
        }**,COMMA**
        {
            "id":"14",
            "ename":"IMDB Top 250","vname":"250 b? phim hay nh?t m?i th?i d?i",
            "movies":"127"
        }
    ]
}

请注意,我只是将字符串COMMA放在地方下划线。您只需添加,而不需要COMMA

答案 1 :(得分:6)

如果要在json中使用引号,则必须使用转义引号。像这样:

[{"vname": "Tuyển tập \"Trởlạitươnglai\""}]

答案 2 :(得分:3)

您的回复必须是这样的

{
    "total": "2",
    "result": [
        {
            "id": "15",
            "ename": "Horror movies based on true stories",
            "vname": "Những phim kinh dị dựa trên chuyện có thật",
            "movies": "16"
        },
        {
            "id": "14",
            "ename": "IMDB Top 250",
            "vname": "250 bộ phim hay nhất mọi thời đại",
            "movies": "127"
        },
        {
            "id": "13",
            "ename": "10 good movies for women",
            "vname": "10 bộ phim hay dành cho phái đẹp",
            "movies": "10"
        },
        {
            "id": "12",
            "ename": "The 84th Annual Academy Awards",
            "vname": "Giải Oscars lần thứ 84 (2012)",
            "movies": "37"
        },
        {
            "id": "11",
            "ename": "Charlie Chaplin collection",
            "vname": "Tuyển tập hề Sác lô",
            "movies": "7"
        },
        {
            "id": "10",
            "ename": "Tuyển tập điệp viên 007",
            "vname": "007 collection",
            "movies": "23"
        },
        {
            "id": "9",
            "ename": "Donnie Yen movies collection",
            "vname": "Tuyển tập phim Chung Tử Đơn",
            "movies": "24"
        },
        {
            "id": "8",
            "ename": "Back to the Future trilogy",
            "vname": "Tuyển tập Trởlạitươnglai",
            "movies": "3"
        },
        {
            "id": "7",
            "ename": "StiegLarssonsMillenniumtrilogy",
            "vname": "BộtiểuthuyếtMilleniumcủanhàvănStiegLarsson",
            "movies": "3"
        },
        {
            "id": "6",
            "ename": "ChanWookParksvengeancetrilogy",
            "vname": "BộbaphimBáothùcủađạodiễnParkChanWook",
            "movies": "3"
        }
    ]
}

此外,您的一个值包含"额外Trở lại tương lai,因此请将其删除。

答案 3 :(得分:2)

这有帮助吗? Android: Json string with spaces gives "Unterminated object at" exception

看起来你需要查看一些JSON数组如何形成的例子。

答案 4 :(得分:2)

这里是json数组,你在Jsonarray result

中的每个元素之后都缺少逗号

您可以检查JSON是否有效herehere

{
  "total": "2",
  "result": [
    {
      "id": "15",
      "ename": "Horror movies based on true stories",
      "vname": "Những phim kinh dị dựa trên chuyện có thật",
      "movies": "16"
    }{
      "id": "14",
      "ename": "IMDB Top 250",
      "vname": "250 bộ phim hay nhất mọi thời đại",
      "movies": "127"
    }{
      "id": "13",
      "ename": "10 good movies for women",
      "vname": "10 bộ phim hay dành cho phái đẹp",
      "movies": "10"
    }{
      "id": "12",
      "ename": "The 84th Annual Academy Awards",
      "vname": "Giải Oscars lần thứ 84 (2012)",
      "movies": "37"
    }{
      "id": "11",
      "ename": "Charlie Chaplin collection",
      "vname": "Tuyển tập hề Sác lô",
      "movies": "7"
    }{
      "id": "10",
      "ename": "Tuyển tập điệp viên 007",
      "vname": "007 collection",
      "movies": "23"
    }{
      "id": "9",
      "ename": "Donnie Yen movies collection",
      "vname": "Tuyển tập phim Chung Tử Đơn",
      "movies": "24"
    }{
      "id": "8",
      "ename": "Back to the Future trilogy",
      "vname": "Tuyển tập "Trởlạitươnglai"",
      "movies": "3"
    }{
      "id": "7",
      "ename": "Stieg Larssons Millennium trilogy",
      "vname": "Bộ tiểu thuyết Millenium của nhà văn Stieg Larsson",
      "movies": "3"
    }{
      "id": "6",
      "ename": "Chan Wook Parks vengeance trilogy",
      "vname": "Bộ ba phim Báo thù của đạo diễn Park Chan Wook",
      "movies": "3"
    }
  ]
}

答案 5 :(得分:0)

我刚刚解决了同样的问题。该代码正在创建一个简单的 json 文件,然后读取相同的 json 文件。 Read 抛出相同的异常。在字节到字符串的转换中,我添加了编码并解决了这个问题。

private void PlayWithJson() {
    File dir = new File(Environment.getExternalStorageDirectory() + "/folder");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    if (dir.exists()) {
    {
        File textfile = new File(Environment.getExternalStorageDirectory() + "/folder/file.json");
        try {
            JSONObject jsonRoot = new JSONObject();
            jsonRoot.put("MagicNumber","8461684368431684684186464");
            jsonRoot.put("Note","develop android is lame");
            FileOutputStream stream = new FileOutputStream(textfile);
            byte[] jsonBytes = jsonRoot.toString().getBytes(Charset.forName("UTF8"));
            stream.write(jsonBytes);
            stream.close();
        } catch (Exception e) {
            Log.d("Write Throw Up",e.getMessage());
        }
    }
    {
        File textfile = new File(Environment.getExternalStorageDirectory() + "/folder/file.json");
        try {
            int length = (int) textfile.length();
            byte[] bytes = new byte[length];
            FileInputStream in = new FileInputStream(textfile);
            in.read(bytes);
            in.close();
            String jsonStr = new String(bytes, Charset.forName("UTF8"));
            JSONObject readJson = new JSONObject(jsonStr);
            String MagicNumber = "";
            String Note = "";
            if (!readJson.optString("MagicNumber").isEmpty()) {
                MagicNumber = readJson.optString("MagicNumber");
            }
            if (!readJson.optString("Note").isEmpty()) {
                Note = readJson.optString("Note");
            }
            String contents = new String(MagicNumber+"\n"+Note);
            Toast.makeText(getApplicationContext(), contents, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.d("Read Throw Up",e.getMessage());
        }
    }
}

希望有用。