[英]Python : Getting values of key from JSON
Wrote a python script to collect the response header of a list of websites.Now,I have json file which has the response header
of website. 编写了一个python脚本来收集网站列表的响应标头。现在,我有一个json文件,其中包含网站的response header
。 1
is the ID (key)
and rest all values
are the response header parsed for a particular URL. 1
是ID (key)
和休息所有values
都是响应报头解析为特定的URL。
{
"1": {
"transfer-encoding": "chunked",
"set-cookie": "lng=hi; path=/; expires=Tue, 14-Feb-2017 08:54:15 GMT; domain=.ucoz.net;, uSID=HYv2D8GG3nmptnLPRZbxuxBHi5awMLA8pwrSRf2aE8t0l9%3BEeTKJkgoo; path=/; domain=www.ucoz.net;",
"keep-alive": "timeout=15",
"server": "nginx/1.8.0",
"connection": "keep-alive",
"date": "Mon, 15 Feb 2016 08:54:15 GMT",
"content-type": "text/html; charset=UTF-8"
}
}
How to get the value of server for key 1
如何获取密钥1
的服务器的值
Result : 结果:
1,nginx/1.8.0
Use JSON encoder to parse data in your file: 使用JSON编码器解析文件中的数据:
import json
from pprint import pprint
with open('json_data.json') as json_data:
data = json.load(json_data)
pprint(data)
# now you can traverse your data dict....
print(data['1']['server'])
json.loads
: Deserialize data
(instance containing a JSON) to a Python object. json.loads
:将data
反序列化(包含JSON的实例)到Python对象。
>>> import json
>>> data = ''' {
... "1": {
... "transfer-encoding": "chunked",
... "set-cookie": "lng=hi; path=/; expires=Tue, 14-Feb-2017 08:54:15 GMT; domain=.ucoz.net;, uSID=HYv2D8GG3nmptnLPRZbxuxBHi5awMLA8pwrSRf2aE8t0l9%3BEeTKJkgoo; path=/; domain=www.ucoz.net;",
... "keep-alive": "timeout=15",
... "server": "nginx/1.8.0",
... "connection": "keep-alive",
... "date": "Mon, 15 Feb 2016 08:54:15 GMT",
... "content-type": "text/html; charset=UTF-8"
... }
... }'''
>>> d = json.loads(data)
>>> d['1']['server']
u'nginx/1.8.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.