简体   繁体   中英

How to parse escaped Json using JSONObject in android

I'm unable to parse escaped json string through JSONObject, while trying to parse it, I always get an Unterminated Object error. But when I manually remove all of the escape characters from the json string, JSONObject parses it successfully.

But the problem is that for complex or nested json strings, I don't want to remove escape characters, because for nested structures, unescaping the characters results in an invalid json string.

This is the json string after removing escapes characters and I get unterminated object error due to invalid json when parsing through JSONObject

{"result":"unknown","message":"{"firebase":"unknown"}","sender":"unknown"}

This is the unescaped json string

{"result":"unknown","message":"{\"firebase\":\"unknown\"}","sender":"unknown"}

If you look at the json you are constructing it in wrong format.

In case of escaped json

{
    "result":"unknown",
    "message":"{"firebase":"unknown"}",
    "sender":"unknown"
}

the json object "message" is having value "{" and firebase is causing exception.

In the the escaped json "message":{"firebase":"unknown"} will form valid json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM