开发者

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

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"&g开发者_如何学Got;
        {"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?


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.


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜