这是运行脚本后得到的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中没有任何东西像“无”。要么你会看到你的数据或未定义。请明确指出您得到的结果 –