2015-04-27 58 views
1

我正在使用Android Studio,並且我有一個名爲sResponse(下面)的字符串變量。根據調試器,它擁有以下值:將字符串轉換爲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/" 
    } 
    } 

當我嘗試以這種方式與初始化一個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在URL中造成了麻煩。我不擅長轉義JSON字符。我如何從前一個字符串獲取有效的JSONObject?我的方法可以發現什麼問題?

+0

你有不當JSON請糾正'的ActionResult ='對象在你的JSON響應您指出的錯誤@NambiNarayanan – Nambi

+0

感謝,價值來源於支持(也許硬編碼?)但請不要低估! – Josh

+0

好的,我刪除了我的投票選項,但在將來發布之前請檢查您的問題。 – Nambi

回答

4

由於=附近的標記由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/

+0

謝謝,你在哪裏,我一直在掙扎幾個小時,我厚厚的眼睛不會看到錯誤!它來自後端。 – Josh

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/" 
    } 
    } 
} 
相關問題