如何从这个list-dict json响应中只提取键值?

时间:2016-08-29 23:11:35

标签: python json list dictionary

我想如何只从这个json响应中提取键和值:

[{u'SkuSellersInformation': [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'AvailableQuantity': 2, u'LogoUrl': None, u'SellerId': u'1', u'ListPrice': 409}], u'BestInstallmentNumber': 10, u'RealWeightKg': 100.0, u'NotifyMe': True, u'HasServiceAtCartPage': False, u'RewardValue': 0.0, u'ListPrice': 409, u'Name': u'Light Blue Eau de Toilette Dolce & Gabbana - Perfume Feminino - 50ml', u'HasExtendedWarranty': False, u'BestInstallmentValue': 40.9, u'Ean': u'0737052074313', u'Price': 409, u'RealWidth': 10.0, u'IdProduct': 909, u'AvailabilityMessage': u'True', u'HasServiceAtServicePage': False, u'RealLength': 10.0, u'RealHeight': 10.0, u'HasServiceAtProductPage': False, u'SalesChannel': u'1', u'DefaultSellerId': u'1', u'Reference': u'002796', u'HasExtendedWarrantyPage': False, u'Id': 5293, u'Images': [[{u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-320-320/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-55-55/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-65-65/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-500-500/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-120-120/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-130-130/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}], [{u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-320-320/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-55-55/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-65-65/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-500-500/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-120-120/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-130-130/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}]], u'Availability': True}]

我试过这种方式,但仍然在子列表中保留了一些词汇

import urllib2
import json

response = urllib2.urlopen('http://www.site.com.br/produto/sku/5293')
data = json.load(response)

for a in data:
    for key, value in a.items():
        print key, value

这是结果,但正如大家们所见,仍然在子列表中保留一些关键值

SkuSellersInformation :  [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'AvailableQuantity': 2, u'LogoUrl': None, u'SellerId': u'1', u'ListPrice': 409}]
BestInstallmentNumber :  10
RealWeightKg :  100.0
NotifyMe :  True
HasServiceAtCartPage :  False
RewardValue :  0.0
ListPrice :  409
Name :  Light Blue Eau de Toilette Dolce & Gabbana - Perfume Feminino - 50ml
HasExtendedWarranty :  False
BestInstallmentValue :  40.9
Ean :  0737052074313
Price :  409
RealWidth :  10.0
IdProduct :  909
AvailabilityMessage :  True
HasServiceAtServicePage :  False
RealLength :  10.0
RealHeight :  10.0
HasServiceAtProductPage :  False
SalesChannel :  1
DefaultSellerId :  1
Reference :  002796
HasExtendedWarrantyPage :  False
Id :  5293
Images :  [[{u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-320-320/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-55-55/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-65-65/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-500-500/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-120-120/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-130-130/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}], [{u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-320-320/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-55-55/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-65-65/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-500-500/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-120-120/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-130-130/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}]]
Availability :  True

2 个答案:

答案 0 :(得分:1)

由于JSON允许定义递归数据结构,因此以下递归函数将在一个字典中找到所有字典中的所有键值对。请注意,当它出现在多个(嵌套或并行)字典中时,它可能会多次产生相同的密钥。

def get_all(myjson):
    """ Recursively find the keys and associated values in all the dictionaries
        in the json object or list.
    """
    if isinstance(myjson, dict):
        for jsonkey, jsonvalue in myjson.items():
            if not isinstance(jsonvalue, (dict, list)):
                yield jsonkey, jsonvalue
            else:
                for k, v in get_all(jsonvalue):
                    yield k, v
    elif isinstance(myjson, list):
        for element in myjson:
            if isinstance(element, (dict, list)):
                for k, v in get_all(element):
                    yield k, v

data = [{u'SkuSellersInformation': [{u'Name': u'site', u'Price': 409, u'IsDefaultSeller': True, u'AvailableQuantity': 2, u'LogoUrl': None, u'SellerId': u'1', u'ListPrice': 409}], u'BestInstallmentNumber': 10, u'RealWeightKg': 100.0, u'NotifyMe': True, u'HasServiceAtCartPage': False, u'RewardValue': 0.0, u'ListPrice': 409, u'Name': u'Light Blue Eau de Toilette Dolce & Gabbana - Perfume Feminino - 50ml', u'HasExtendedWarranty': False, u'BestInstallmentValue': 40.9, u'Ean': u'0737052074313', u'Price': 409, u'RealWidth': 10.0, u'IdProduct': 909, u'AvailabilityMessage': u'True', u'HasServiceAtServicePage': False, u'RealLength': 10.0, u'RealHeight': 10.0, u'HasServiceAtProductPage': False, u'SalesChannel': u'1', u'DefaultSellerId': u'1', u'Reference': u'002796', u'HasExtendedWarrantyPage': False, u'Id': 5293, u'Images': [[{u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-320-320/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-55-55/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-65-65/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-500-500/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-120-120/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/183280-130-130/light-blue-edt-dg-2.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'183280', u'IsMain': False}], [{u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-320-320/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 2, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-55-55/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 3, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-65-65/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 1, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-500-500/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 10, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-120-120/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 29, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}, {u'Path': u'http://site.vteximg.com.br/arquivos/ids/187857-130-130/light-blue-eau-de-toilette-dolce-gabbana-perfume-feminino.jpg', u'ArchiveTypeId': 30, u'Name': None, u'IdArchive': u'187857', u'IsMain': True}]], u'Availability': True}]

for key, value in get_all(data):
    print('{!r}: {!r}'.format(key, value))

答案 1 :(得分:-1)

那么,

你的问题不明确,但也许,这就是你想要的:

for a in data:
    for key, value in a.items():
        if key in ['BestInstallmentNumber', 'RealWeightKg', 'NotifyMe', 'HasServiceAtCartPage', 'RewardValue']:
            print key, value