Josh
Josh

Reputation: 6373

Unterminated object when converting a String to JSON

I am working with Android Studio, andI have a string variable, called sResponse (below). According to the debugger, it holds the following value:

 {
    "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/"
    }
    }

When I try to initialize a JSONObject with it with in this way:

jResponse=new JSONObject(sResponse);

...The following exception rises in my Logcat:

>>>>>>>>>Thread EXCEPTION1: Response with invalid JSON format: , FrontendActivity.java L:421 ***** *org.json.JSONException: Unterminated object at character 502 of : sResponse

I suspect that those // in the URL are causing trouble. I am no expert in escaping JSON Characters. How can I obtain a valid JSONObject from the previous string? What trouble can you spot in my approach?

Upvotes: 2

Views: 2039

Answers (2)

Amy
Amy

Reputation: 4032

Problem caused because of = sign near by actionResult as well as actionResult not surrounded with double quotes and you didn't close json string properly.

Replace Json String With:

{
    "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/"
    }
    }
}

and add } at the end of the string.

You can track the error using following online tool:

http://json.parser.online.fr/

Upvotes: 4

Kirankumar Zinzuvadia
Kirankumar Zinzuvadia

Reputation: 1249

You missed last closing curly at the end of the response. Just add } on last line.

Corrected json Response

{
  "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/"
    }
  }
}

Upvotes: 1

Related Questions