gpt4 book ai didi

java - 将字符串转换为 JSON 时未终止的对象

转载 作者:太空狗 更新时间:2023-10-29 16:35:34 24 4
gpt4 key购买 nike

我正在使用 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/

关于java - 将字符串转换为 JSON 时未终止的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com