Android: Json string with spaces gives an “Unterminited 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> 

The following message appears:

 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 has no problems:

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

Am I quoting something wrong?

EDIT: After reading my own post, I noticed that the error message does not contain quotes around the values ​​of string objects. So I changed the "to" in the xml line and worked fine. Any idea how to do this does not remove quotes?

+7
source share
3 answers

After reading my own post, I noticed that the error message does not contain quotation marks around the values ​​of the string objects. So I changed the "to" in the xml line and worked fine.

+7
source

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> 
+1
source

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

0
source

Source: https://habr.com/ru/post/896750/


All Articles