7

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?

Andrew Boes
  • 2,013
  • 4
  • 22
  • 29

3 Answers3

7

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.

Andrew Boes
  • 2,013
  • 4
  • 22
  • 29
1

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

1

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>
Femi
  • 64,273
  • 8
  • 118
  • 148