2015-08-16 64 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兼容對象。

相關問題