2013-05-28 55 views
0

我想從JSON中提取一個值。如何解析並從python中獲取JSON結果

search_results = urllib2.urlopen(request) 
for result in search_results: 
    print result; 

打印結果

{"preview":false,"offset":1,"lastrow":true,"result":{"_bkt":"main~48~BF0767A8-59E9-4783-BB86-F5693835BF2F","_cd":"48:395947656","_indextime":"1369783573","_raw":"2013-05-28 23:05:43 3094 100.244.106.180 200 TCP_NC_MISS 321 967 GET http www.naver.com 80 /include/themecast/menu_count.json - - - - www.naver.com text/html;%20charset=UTF-8 \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/277.0.1453.93 Safari/537.36\" OBSERVED \"Central_AllWhiteList\" - 104.244.128.30 SG-HTTP-Service 55707 - \"Central_AllWhiteList;Central_AllDoNotAuth;Search Engines/Portals\" http://www.naver.com/ json http://www.naver.com/include/themecast/menu_count.json 63.80.4.67 www.naver.com","_serial":"1","_si":["test.com","main"],"_sourcetype":"Bluecoat","_time":"2013-05-28 16:05:43.000 PDT","host":"dssd-sdfsd-001","index":"main","linecount":"1","source":"D:\\OutSPLNK\\SG__10_244_128_30__052813__230919.log.gz","sourcetype":"sadqw","splunk_server":"test.com"}} 

我想從鍵提取值「_raw」] 什麼是做到這一點的呢?

+0

JSON *是*文字和'urlopen'返回一個字符串。在可以輕鬆訪問組件之前,必須將JSON解析爲對象模型(使用庫)。瞭解這個過程,可以通過「解析JSON」找到許多有用的結果。 – user2246674

回答

5

喜歡的東西:

import json 
json_data = json.loads(result) 
stuff = json_data['result']['_raw']