在不知道键值的情况下解析JSON

时间:2018-09-19 20:14:05

标签: python json

我知道如何解析知道键值的JSON,但是现在我想从不是我的JSON中获取键值,所以我可以知道键名,例如我有这个JSON

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  ...
 ]

所以从现在开始我有这个:

with open('users.json') as f:
    data = json.load(f)

如果我打印data,在哪里可以看到所有加载的JSON,所以我的问题是,如何在不知道名称的情况下打印所有键和嵌套对象?

我的目标是拥有类似 ID 名称 用户名 电子邮件 包含街道,套房,城市,邮政编码,包含经纬度,经度等的地理位置的地址。

2 个答案:

答案 0 :(得分:3)

这是一个递归生成器,它将扫描嵌套的列表/字典结构,就像将JSON加载到Python中一样。它显示了与每个值关联的字典键和列表索引的顺序。

我已经稍微修改了您的数据,以说明其如何处理嵌套在字典中的列表。

data = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    },
    "other": ["This", "is", "a list"]
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    },
    "other": ["This", "is", "another list"]
  },
]    

def show_indices(obj, indices):
    for k, v in obj.items() if isinstance(obj, dict) else enumerate(obj):
        if isinstance(v, (dict, list)):
            yield from show_indices(v, indices + [k])
        else:
            yield indices + [k], v

for keys, v in show_indices(data, []):
    print(keys, v)

输出

[0, 'id'] 1
[0, 'name'] Leanne Graham
[0, 'username'] Bret
[0, 'email'] Sincere@april.biz
[0, 'address', 'street'] Kulas Light
[0, 'address', 'suite'] Apt. 556
[0, 'address', 'city'] Gwenborough
[0, 'address', 'zipcode'] 92998-3874
[0, 'address', 'geo', 'lat'] -37.3159
[0, 'address', 'geo', 'lng'] 81.1496
[0, 'phone'] 1-770-736-8031 x56442
[0, 'website'] hildegard.org
[0, 'company', 'name'] Romaguera-Crona
[0, 'company', 'catchPhrase'] Multi-layered client-server neural-net
[0, 'company', 'bs'] harness real-time e-markets
[0, 'other', 0] This
[0, 'other', 1] is
[0, 'other', 2] a list
[1, 'id'] 2
[1, 'name'] Ervin Howell
[1, 'username'] Antonette
[1, 'email'] Shanna@melissa.tv
[1, 'address', 'street'] Victor Plains
[1, 'address', 'suite'] Suite 879
[1, 'address', 'city'] Wisokyburgh
[1, 'address', 'zipcode'] 90566-7771
[1, 'address', 'geo', 'lat'] -43.9509
[1, 'address', 'geo', 'lng'] -34.4618
[1, 'phone'] 010-692-6593 x09125
[1, 'website'] anastasia.net
[1, 'company', 'name'] Deckow-Crist
[1, 'company', 'catchPhrase'] Proactive didactic contingency
[1, 'company', 'bs'] synergize scalable supply-chains
[1, 'other', 0] This
[1, 'other', 1] is
[1, 'other', 2] another list

您可以使用这些列表访问任何项目,例如

keys = [1, 'company', 'catchPhrase']
obj = data
for k in keys:
    obj = obj[k]
print(obj)

输出

Proactive didactic contingency

或者如果您要修改项目:

keys = [1, 'company', 'catchPhrase']
obj = data
for k in keys[:-1]:
    obj = obj[k]
obj[keys[-1]] = "some new thing"
print(data[1]['company'])

输出

{'name': 'Deckow-Crist', 'catchPhrase': 'some new thing', 'bs': 'synergize scalable supply-chains'}

答案 1 :(得分:0)

尝试这样的方式

API.get('my-api', '/path', { headers: { 'Content-Type': 'application/json' } })