Android: Json string with spaces gives “Unterminated object at” exception

前端 未结 3 648
萌比男神i
萌比男神i 2020-12-11 05:25

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);
         


        
相关标签:
3条回答
  • 2020-12-11 05:29

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

    0 讨论(0)
  • 2020-12-11 05:34

    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>
    
    0 讨论(0)
  • 2020-12-11 05:44

    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.

    0 讨论(0)
提交回复
热议问题