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 !

4
  • 5
    have you checked your json in jsonlint.com Commented Apr 26, 2012 at 13:27
  • it will tell you your json is valid or not Commented Apr 26, 2012 at 13:28
  • 1
    i think every object should separate by comma right? Commented Apr 26, 2012 at 13:32
  • caution. for him the two character points means value of the JSON key. you have to find the way to incorporate everything into a string Commented Aug 8, 2018 at 9:32

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

hahaha 1 second faster than me. Yes the comma is missing in the json array
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\""}]

Comments

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.

1 Comment

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 {}
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.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.