I am parsing an array of json elements on android. Following line generates following exception. I've validated the json file on some validation pages and all seems correct. What's wrong?
JSONObject object = new JSONObject(content);
The exception:
org.json.JSONException: Unterminated array at character 21 of {
"info":[
{
"category":"Cocktail",
"text":"Long Island Ice Tea",
"info":"nur am 19. Juni",
"imageUrl":"http://www.google.at/google.png",
"thumbnailUrl":"http://www.google.at/google.png"
},
{
"category":"Grill-Wochen",
"text":"Steak vom Grill 350g",
"info":"AB 16.09.2012",
"imageUrl":"http://www.example.com/example.jpg",
"thumbnailUrl":"http://www.example.com/example_thumb.jpg"
}
]
}
2 Answers 2
I used a JSON parser online and checked your string. It does have a syntax error.
Analyzing the JSON syntax at JSON.org we can verify that every object must start and finish with curved brackets ("{"). "Info" is an object - so it should be stated as so. I changed your code to
{ "info":[
{
"category":"Cocktail",
"text":"Long Island Ice Tea",
"info":"nur am 19 Juni",
"imageUrl":"http://www.google.at/google.png",
"thumbnailUrl":"http://www.google.at/google.png"
},
{
"category":"Grill-Wochen",
"text":"Steak vom Grill 350g",
"info":"AB 16.09.2012",
"imageUrl":"http://www.example.com/example.jpg",
"thumbnailUrl":"http://www.example.com/example_thumb.jpg"
}
]
}
and the parser was happy with it.
I hope it helps
I solved the problem. I've checked the file for some encoding problem. After downloading the file from the apache server with the input stream, it contained some broken chars.
replaceAll("\\n", "")on the json string before passing to theJSONObjectclass. Hope it helps.