在不知道键值的情况下解析JSON [英] parse JSON without knowing the key value

查看:101
本文介绍了在不知道键值的情况下解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to parse a JSON knowing the key value, but now I'd like to get the key values from a JSON that it's not mine, so I can know the key names, for instance I have this 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"
    }
  },
  ...
 ]

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

So from now I have this :

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

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

Where I can see all the JSON loaded if I print data, so my question is, how do I print all of the keys and nested objects without knowing the name?

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

My goal is having something like id name username email address that contains street, suite, city, zipcode, geo that contains lat, long, etc..

推荐答案

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

Here's a recursive generator that will scan through a nested list / dictionary structure, like you get from loading JSON into Python. It shows you the sequence of dictionary keys and list indices associated with every value.

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

I've modified your data slightly to illustrate how it handles lists nested inside dicts.

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


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


You can use these lists to access any item, eg

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

输出

Proactive didactic contingency

或者如果您要修改项目:

Or if you want to modify an item:

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'}

这篇关于在不知道键值的情况下解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆