2017-06-23 152 views
0

我目前遇到問題,無法找到解決方案。如何訪問json數據

我做一個REST服務的請求,並抓住一些數據...

var test = responseData._bodyInit; 
console.log(test); 

06-23 03:43:14.383 12800 12832 I ReactNativeJS: {"search_time": 92, "results": [{"item": {"name": "Profil", "url": "https://www.someurl.tdl", "custom": "bla", "content": null, "trackable": false, "uuid": "otheruuid"}, "image": {"thumb_120": "morebla", "uuid": "someuuid"}, "score": 69}]} 

到目前爲止好,但我只希望有「URL」 ......所以我嘗試:

console.log(test['results'][0].item.url); 

,並收到以下日誌:

06-23 03:43:14.398 12800 12832 I ReactNativeJS: { [TypeError: undefined is not an object (evaluating 'test['results'][0]')] 

06-23 03:43:14.398 12800 12832 I ReactNativeJS: line: 1340, 

06-23 03:43:14.398 12800 12832 I ReactNativeJS: column: 40, 

06-23 03:43:14.398 12800 12832 I ReactNativeJS: sourceURL: 'http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false' } 
+0

也許'test'是一個JSON字符串,尚未解析?試試'JSON.parse(test).results [0] .item.url'而不是? – smarx

+0

或者嘗試'console.log(typeof(test))'來檢查。 – smarx

+0

謝謝!它是字符串。 – Marq

回答

0

這將做到這一點:

var url = JSON.parse(j).results[0].item.url;