将字符串转换为 JSON 时未终止的对象
Unterminated object when converting a String to JSON
我正在使用 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/"
}
}
当我尝试用这种方式初始化一个 JSON 对象时:
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/"
}
}
}
并在字符串末尾添加 }
。
您可以使用以下在线工具跟踪错误:
您错过了回复末尾的最后一个结束卷曲。
只需在最后一行添加 }
。
已更正 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/"
}
}
}
我正在使用 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/"
}
}
当我尝试用这种方式初始化一个 JSON 对象时:
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/"
}
}
}
并在字符串末尾添加 }
。
您可以使用以下在线工具跟踪错误:
您错过了回复末尾的最后一个结束卷曲。
只需在最后一行添加 }
。
已更正 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/"
}
}
}