Android:带空格的Json字符串给出“Unterminated object at”异常

时间:2011-09-06 22:49:09

标签: android json

每当我有一个带有空格的Json字符串对象时,我都会收到以下错误。

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>

我收到以下消息:

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}]} 

这没有任何问题:

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

我引用了错误的内容吗?

编辑:通过我自己的帖子阅读我注意到错误消息没有任何关于字符串对象值的引号。所以我在xml字符串中更改了“to”,它运行正常。知道怎么让它不删除任何引号?

3 个答案:

答案 0 :(得分:7)

通过我自己的帖子阅读我注意到错误消息没有任何关于字符串对象值的引号。所以我在xml字符串中更改了“to”,它运行正常。

答案 1 :(得分:1)

尝试将其包装在CDATA块中。这可以防止任何混淆。

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

答案 2 :(得分:0)

JSON中的空格会造成此问题。尝试跟随json {“ Array”:[{“ Name”:“一二三”“},{” Name“:” Two“},{” Name“:” Three“}]}