java JSON 异常:org.json.JSONException:未终止的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10334385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON exception: org.json.JSONException: Unterminated array
提问by hungson175
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:
我正在开发一个 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"
}
]
}
I've searched in internet, but no luck. And I also count to character 152th, but nothing wrong ! Please help me !
我在互联网上搜索过,但没有运气。我也数到第 152 个字符,但没有错!请帮我 !
回答by Boris Strandjev
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
.
请注意,我放置字符串COMMA
只是为了在该位置下划线。您只需要添加,
不带COMMA
.
回答by UGD
When you want to use quotes in your json you must must use escaped quotes. Like this:
当您想在 json 中使用引号时,您必须使用转义引号。像这样:
[{"vname": "Tuy?n t?p \"Tr?l?it??nglai\""}]
回答by Mohammed Azharuddin Shaikh
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 co? tha?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.
而且你的一个值包含"
额外的 Tr? l?i t??ng lai
所以只需删除它。
回答by John Sykor
Does this help? Android: Json string with spaces gives "Unterminated object at" exception
这有帮助吗? Android:带空格的 Json 字符串给出“未终止的对象在”异常
Seems like you need to look up some examples of how JSON arrays are formed.
似乎您需要查找一些有关如何形成 JSON 数组的示例。
回答by Akram
here is you json array you are missing comma after each element in your Jsonarray result
这是你的 json 数组,你的 Jsonarray 中的每个元素后面都缺少逗号 result
you can check you JSON whether its valid or not hereor 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 co? tha?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"
}
]
}