4

I'm working on an android app - which requests some data from server and the server returns data in JSON format. Everything works fine, except an API. After debugging, I've found this Exception:

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

I've searched in internet, but no luck. And I also count to character 152th, but nothing wrong ! Please help me !

Tarec
  • 3,268
  • 4
  • 30
  • 47
hungson175
  • 4,373
  • 6
  • 22
  • 21

6 Answers6

10

I see something wrong:

The elements in the array should be separated with commas:

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

Note that I placed the string COMMA just to underline the place. You need to add only , without COMMA.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
6

When you want to use quotes in your json you must must use escaped quotes. Like this:

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

your response must be like this

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

and moreover your one value contains " extra Trở lại tương lai so just remove that.

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • `and moreover your on value contains " extra Trở lại tương lai so just remove that.` you can escape the `"` like`\"`and the above is correct that you are missing commas between {} – Andrei Sfat Apr 26 '12 at 13:37
2

Does this help? Android: Json string with spaces gives "Unterminated object at" exception

Seems like you need to look up some examples of how JSON arrays are formed.

Community
  • 1
  • 1
John Sykor
  • 727
  • 5
  • 15
2

here is you json array you are missing comma after each element in your Jsonarray result

you can check you JSON whether its valid or not here or here

{
  "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"
    }
  ]
}
Akram
  • 7,548
  • 8
  • 45
  • 72
0

I just solved the same problem. The code is creating a simple json file an then reads the same json file. Read was throwing the same Exception. In bytes conversion to string I added encoding and it solved the issue.

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

I hope it's useful.

Rolas
  • 133
  • 8