這是運行腳本後得到的json對象。如何使用javascript從json對象中提取項目
{
"log": {
"entries": [{
"startedDateTime": "2015-08-16T10:27:35.264Z",
"time": 35,
"request": {
"method": "GET",
"url": "http://www.google.com/",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [{
"name": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.8 Safari/534.34"
}, {
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}],
"queryString": [],
"headersSize": -1,
"bodySize": -1
},
"response": {
"status": 302,
"statusText": "Found",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [{
"name": "Cache-Control",
"value": "private"
}, {
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}, {
"name": "Location",
"value": "http://www.google.co.in/?gfe_rd=cr&ei=mWXQVZiNLaHv8wehp6jYDw"
}, {
"name": "Content-Length",
"value": "261"
}, {
"name": "Date",
"value": "Sun, 16 Aug 2015 10:27:37 GMT"
}, {
"name": "Server",
"value": "GFE/2.0"
}, {
"name": "Connection",
"value": "keep-alive"
}],
"redirectURL": "",
"headersSize": -1,
"bodySize": 261,
"content": {
"size": 261,
"mimeType": "text/html; charset=UTF-8"
}
},
"cache": {},
"timings": {
"blocked": 0,
"dns": -1,
"connect": -1,
"send": 0,
"wait": 35,
"receive": 0,
"ssl": -1
},
"pageref": "http://www.google.com"
}, .....
]
}
}
在我的JavaScript,我試圖訪問每個對象。但它不工作。
比方說,例如,我這個JSON對象分配給數據:
data = JSON.parse({... that whole json object...});
console.log(data["log"]["entries"][0]);
我什麼也沒得到。我正在使用這個內部節點。我在這裏犯了什麼錯誤?
你什麼也得不到或類似'[Object對象]'什麼? – AmmarCSE
將'console.log(data)'的輸出放在' – Abhi
中。JavaScript中沒有任何東西像「無」。要麼你會看到你的數據或未定義。請明確指出您得到的結果 –