[英]Not able to read json property in javascript
我從API得到此響應:
var ob = {"bytesSent":1087,"responseCode":200,"response":"{\"id\":\"4b1e9740-6059-11e5-9454-518e45576d76\"}","objectId":""}
如果我做ob.response.id
它顯示未定義。 可能是什么把戲?
我嘗試了var rss=JSON.parse(ob);
但顯示SyntaxError: Unexpected token o
response
是您的api響應中的另一個未解析的JSON(字符串)。 您需要解析response
:
var ob = {"bytesSent":1087,"responseCode":200,"response":"{\"id\":\"4b1e9740-6059-11e5-9454-518e45576d76\"}","objectId":""}
var ob2 = JSON.parse(ob.response);
console.log(ob2.id);
問題是ob.response
是JSON string
而不僅僅是JSON
。
var ob = { "bytesSent": 1087, "responseCode": 200, "response": "{\\"id\\":\\"4b1e9740-6059-11e5-9454-518e45576d76\\"}", "objectId": "" }; alert(typeof ob.response); alert(JSON.parse(ob.response).id)
您需要更改Ob變量,請檢查以下代碼。
var ob = {"bytesSent":1087,"responseCode":200,"response":{"id":"4b1e9740-6059-11e5-9454-518e45576d76"},"objectId":""}
alert(ob.response.id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.