Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Json string with spaces gives "Unterminated object at" exception

Tags:

json

android

Whenever I have a Json string object with a space in it I get the following error.

Java:

String jString = getResources().getString(R.string.event);
JSONObject object = new JSONObject(jString);

Json:

<resources>
    <string name="event">
        {"Array":[{"Name":"One two three"},{"Name":"Two"},{"Name":"Three"}]}
    </string>
</resources>

I am get the following message:

09-06 22:35:08.214: WARN/System.err(1079): org.json.JSONException: Unterminated object at character 21 of {Array:[{Name:One two three},{Name:Two},{Name:Three}]} 

This doesn't have any issues:

<resources>
    <string name="event">
        {"Array":[{"Name":"One"},{"Name":"Two"},{"Name":"Three"}]}
    </string>
</resources>

Am I quoting something wrong?

EDIT: Reading through my own post I noticed that the error message doesn't have any quotes around the string object values. So I changed the " to \" in the xml string and it worked fine. Any idea how to have it not remove any quotes?

like image 860
Andrew Boes Avatar asked Sep 06 '11 22:09

Andrew Boes


3 Answers

Reading through my own post I noticed that the error message doesn't have any quotes around the string object values. So I changed the " to \" in the xml string and it worked fine.

like image 124
Andrew Boes Avatar answered Nov 18 '22 14:11

Andrew Boes


Try wrapping it in a CDATA block. This should prevent any confusion.

<resources>
    <string name="event"><![CDATA[
        {"Array":[{"Name":"One two three"},{"Name":"Two"},{"Name":"Three"}]}
    ]]></string>
</resources>
like image 23
Femi Avatar answered Nov 18 '22 15:11

Femi


Space in the JSON creates this issue . TRY following json {"Array":[{"Name":"One-two-three"},{"Name":"Two"},{"Name":"Three"}]}

like image 1
user1252170 Avatar answered Nov 18 '22 16:11

user1252170