Org.json.JSONException: Unterminated object at character 14

I got an error message:

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

I think I should avoid the string, but in vain by trying the whole method in StackOverflow.
Here is my code, thanks for the 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 like “test” or “yo” or “123”, everything works fine. Thanks again!!

+7
source share
3 answers

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

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

Annoying. I wonder if there is a better solution.

EDIT:

, , getString, ,

, . .

, , , .

+14

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

json parser {: yo test}, json, {: "yo test" }.

+1

, , JSON. . ( gson)

0

All Articles