2015-08-16 63 views
-1

这是运行脚本后得到的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]); 

我什么也没得到。我正在使用这个内部节点。我在这里犯了什么错误?

+0

你什么也得不到或类似'[Object对象]'什么? – AmmarCSE

+0

将'console.log(data)'的输出放在' – Abhi

+0

中。JavaScript中没有任何东西像“无”。要么你会看到你的数据或未定义。请明确指出您得到的结果 –

回答

1

你应该把这个作为对象,这里是工作提琴:

https://jsfiddle.net/8jqvvmc6/3/

var jsonData = { 
    "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" 
     }] 
    } 
}; 
alert(jsonData["log"]["entries"][0]); 
0

查看fiddle看看它应该如何工作在你的情况。我还假设data应该是一个局部变量,它可能会在代码执行之前被覆盖。不要污染我猜测的全球范围。

0

这不行!

data = JSON.parse({... that whole json object...}); 

因为

data = {... that whole json object...}; 

已经是一个对象。

但如果你有

data = JSON.parse('{... that whole json object...}'); 

那么就应该首先分析,将字符串转换成JSON兼容对象。

相关问题