11

The error message I got is:

org.json.JSONException: Unterminated object at character 14 of {address: yo test}

I think that I should escape the string, but in vain after trying all the method on StackOverflow.
Here is my code, thanks a lot for any help:

// src/Activity.java
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{
        String str = getString(R.string.data);
        JSONObject jsonObj = new JSONObject(str);
    }
    catch(Exception e){
        Log.d("iLoveDrinkActivity", e.toString());
        // org.json.JSONException: Unterminated object at character 14 of {address: yo test}
    }
}

And...

// res/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="data">{"address": "yo test"}</string>
</resources>

If the "address" is something else like "test" or "yo" or "123", everything works fine. Thanks again!!

iForests
  • 6,757
  • 10
  • 42
  • 75
  • It looks like you just have an unescaped string. What happened when you tried to escape it/how did you try to escape it? – Matt Fenwick Nov 26 '11 at 13:54
  • I have tried "java.net.URLEncoder.encode(queryStr, "UTF-8");", "TextUtils.htmlEncode(queryStr);", and <![CDATA[str]]>, but got the same result. In fact, I am not sure what should I do to escape the string. – iForests Nov 26 '11 at 14:15

4 Answers4

14

The only solution I can find (Android: Json string with spaces gives "Unterminated object at" exception) is to replace the quotes in your json with escaped quotes

<string name="data">{"address": \"yo test\"}</string>

Annoying though. Wonder if there's a better solution.

EDIT:

After a little more digging it looks like the culprit is the getString method which claims to

Return the string value associated with a particular resource ID. It will be stripped of any styled text information.

The stripping of styled text occurs in native code so I cannot see why it throws out the quotes, but looks like it does.

Community
  • 1
  • 1
I82Much
  • 26,901
  • 13
  • 88
  • 119
2

Not directly related to the question, but I got this error when it was trying to parse malformed json. I was missing a comma between two properties. (Using gson)

jgreen
  • 1,132
  • 2
  • 14
  • 18
0

I experienced a similar problem.

Below is a request with an error.

https://.../getInfo?...&DATA=[{...,KEY="2500000001”}]

Can you notice that?

Special symbol ” were included.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30800257) – Alexey Veleshko Jan 16 '22 at 18:39
0

Use

<string name="data">{"address": \"yo test\"}</string>

Otherwise json parser will process the string {address: yo test}, that is not a valid json string, while {address: "yo test"} is.

Rainbowbreeze
  • 1,503
  • 1
  • 9
  • 14
  • Yes, it works. But what can I do if I cannot change the data in strings.xml? Thanks. – iForests Nov 26 '11 at 14:23
  • @Rainbowbreeze how can change this dynamically from code – gandhi Jan 16 '15 at 09:53
  • in my fcm response "url"=http://fdssafas when i hit send message in my FirebaseMessageService onMessageRecived with Exception: Unterminated object at character 36 of..please help me – Harsha Nov 23 '16 at 06:36