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?

1
  • remove spaces and check once array name one Commented Nov 23, 2016 at 11:37

3 Answers 3

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.

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

Comments

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>

Comments

1

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

1 Comment

If the json is served from server and the values contains spaces between them, what should we do?

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.