将String转换为JSON时未终止的对象

时间:2015-04-27 11:55:02

标签: java android json escaping jsonobject

我正在使用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?我的方法有什么麻烦?

2 个答案:

答案 0 :(得分:4)

.vjs-control-bar { overflow: hidden; } 附近的=符号引起的问题以及actionResult未被双引号括起而您没有正确关闭json字符串。

将Json字符串替换为:

actionResult

并在字符串末尾添加{ "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/

答案 1 :(得分: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/"
    }
  }
}