2017-10-08 88 views
-1

hello 從2-3天開始,iam陷入了一個新問題,實際上我必須使用'api url'來檢索複雜的json數組,它就像這樣 -如何從複雜json訪問元素

{"response":{"success":true,"result":{"id":"45203511","device_id":"62970","message":"Rs. 20.00 refunded in your Paytm wallet for your order on Paytm. Updated balance:Rs. 20.0. Queries? Visit Paytm.com\/care.","status":"received","send_at":0,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":0,"canceled_at":0,"failed_at":0,"received_at":1507375388,"error":"N\/A","created_at":1507375388,"contact":{"id":"9209301","name":"VK-IPAYTM","number":"VK-IPAYTM"}}},"status":200} 

我想從此json打印一個元素到控制檯 「console.log(data);」 此代碼在控制檯中打印整個數組,但我必須僅打印「id」元素。 請幫助我!

回答

1

「我只打印‘身份證’元素」

你的數據是嵌套對象,因此要獲得任何對象的屬性,你可以做object.propertyobject["property"]。這裏是一個例子,如何讓id屬性:

var obj = {"response":{"success":true,"result":{"id":"45203511","device_id":"62970","message":"Rs. 20.00 refunded in your Paytm wallet for your order on Paytm. Updated balance:Rs. 20.0. Queries? Visit Paytm.com/care.","status":"received","send_at":0,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":0,"canceled_at":0,"failed_at":0,"received_at":1507375388,"error":"N/A","created_at":1507375388,"contact":{"id":"9209301","name":"VK-IPAYTM","number":"VK-IPAYTM"}}},"status":200} 
 
console.log(obj.response.result.id)

+0

**它顯示的錯誤: - ** _train2.php:23遺漏的類型錯誤:無法讀取屬性 '結果' undefined at Object.fireWith [as resolveWith](jquery.min.js:2) at A(jquery.min)Object.success(train2.php:23) at i(jquery.min.js:2) .js:4) at XMLHttpRequest。 (jquery.min.js:4)_ **我的整個腳本部分是: - ** '' **請幫幫我!** –

+0

@Kalpitrathore,你忘了使用JSON.parse(yourJSONHere)解析你的JSON嗎? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse –