我正在使用Android Studio,我有一个字符串变量,叫做sResponse(下图)。根据调试器,它保存以下值:
{
"responseData": {
"emotion":"",
"lastinput":{actionResult={"value":{"label":"green","key":"1"},"result":"success","action":"displayClickableList"},
"answer":"Sorry, I did not understand.",
"link": {
"href":"",
"target":""
},
"extraData": {
},
"responseSession": {
"id":"c4a5ef257851a991eb32c69132c9",
"transaction":"4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/moto-1/"
}
}
当我尝试以这种方式初始化 JSONObject 时:
jResponse=new JSONObject(sResponse);
。在我的 Logcat 中出现以下异常:
>>>>>>>>>Thread EXCEPTION1: Response with invalid JSON format: , FrontendActivity.java L:421 ***** *org.json.JSONException: Unterminated object at character 502 of : sResponse
我怀疑 URL 中的那些//正在造成麻烦。我不是转义 JSON 字符的专家。如何从上一个字符串中获取有效的 JSONObject?你能在我的方法中发现什么麻烦?
由于actionResult
附近的符号=
以及actionResult
没有用双引号括起来并且您没有正确关闭 json 字符串而导致的问题。
将 JSON 字符串替换为:
{
"responseData": {
"emotion":"",
"lastinput":{"actionResult":{"value":{"label":"green","key":"1"},"result":"success","action":"displayClickableList"},
"answer":"Sorry, I did not understand.",
"link": {
"href":"",
"target":""
},
"extraData": {
},
"responseSession": {
"id":"c4a5ef257851a991eb32c69132c9",
"transaction":"4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/role-va-1/"
}
}
}
并在字符串末尾添加}
。
您可以使用以下在线工具跟踪错误:
http://json.parser.online.fr/
您错过了响应末尾的最后一个收盘卷曲。只需在最后一行添加}
即可。
更正的 json 响应
{
"responseData": {
"emotion": "",
"lastinput": {
actionResult: {
"value": {
"label": "green",
"key": "1"
},
"result": "success",
"action": "displayClickableList"
},
"answer": "Sorry, I did not understand.",
"link": {
"href": "",
"target": ""
},
"extraData": {
},
"responseSession": {
"id": "c4a5ef257851a991eb32c69132c9",
"transaction": "4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/moto-1/"
}
}
}