从Python中的JSON中的特定键中提取特定值

时间:2016-09-26 07:25:58

标签: json list python-2.7 dictionary

所以在网站上搜索后我找不到解决问题的方法, 我想从Jason对象中提取特定值到专用列表。我的代码看起来像这样:

 url2 = 'https://pqrsiem/api/reference_data/sets/clearsky_DOMAIN?fields=data(first_seen)'

request = urllib2.Request(url2,headers=headers)

response = urllib2.urlopen(request)

parsed_response = json.loads(response.read().decode('utf-8'))

print(json.dumps(parsed_response, indent=4))

输出如下:

{
"data": [
    {
        "first_seen": 1474468912626
    }, 
    {
        "first_seen": 1474468912694
    }, 
    {
        "first_seen": 1474468912762
    }, 
    {
        "first_seen": 1474468912831
    }, 
    {
        "first_seen": 1474468912899
    }, 
    {
        "first_seen": 1474468912962
    }, 
    {
        "first_seen": 1474468913022
    }, 
    {
        "first_seen": 1474468913081
    }, 
    {
        "first_seen": 1474468913143
    }, 
    {
        "first_seen": 1474468913203
    }, 
    {
        "first_seen": 1474468913264
    }, 
    {
        "first_seen": 1474468913327
    }, 
    {
        "first_seen": 1474468913388
    }, 
    {
        "first_seen": 1474468913450
    }, 
    {
        "first_seen": 1474468913548
    }, 
    {
        "first_seen": 1474468913626
    }, 
    {
        "first_seen": 1474468913686
    }, 
    {
        "first_seen": 1474468913745
    }, 
    {
        "first_seen": 1474468913806
    }, 
    {
        "first_seen": 1474468913867
    }, 
    {
        "first_seen": 1474468913931
    }, 
    {
        "first_seen": 1474468913992
    }, 
    {
        "first_seen": 1474468914054
    }, 
    {
        "first_seen": 1474468914117
    }, 
    {
        "first_seen": 1474468914178
    }, 
    {
        "first_seen": 1474468914239
    }, 
    {
        "first_seen": 1474468914298
    }, 
    {
        "first_seen": 1474468914357
    }
]

}

我想从“first_seen”键中提取值 我该怎么做????

2 个答案:

答案 0 :(得分:0)

我认为你可以尝试这样的事情:

>>> import json
>>>
>>>
>>> json_data = '{"data": [{"first_seen": 1474468912626}, {"first_seen": 1474468912694}, {"first_seen": 1474468912762}]}'
>>> parsed_response = json.loads(json_data)
>>>
>>>
>>> for element in parsed_response['data']:
...     print element['first_seen']
...
1474468912626
1474468912694
1474468912762

答案 1 :(得分:0)

如果你的json存储在parsed_response中:

for i in range (len(parsed_response["data"])):
    print json["data"][i]["first_seen"]