Python lấy tất cả các khóa từ json lồng nhau

Vấn đề. Bạn có một cấu trúc từ điển lớn (băm) với nhiều cấp độ lồng nhau, các giá trị dưới dạng danh sách và từ điển lồng nhau,

# sample.json{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName", "address", "age"]
}

Cố gắng rút ra giá trị từ điều này có thể là cách thông thường, nghĩa là,

import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])

có thể hoạt động, nhưng điều gì sẽ xảy ra nếu khóa bạn đang cố lấy không tồn tại hoặc có giá trị Không có (trống) hoặc nếu bạn đang duyệt qua một danh sách thì chỉ mục không có ở đó

Bạn sẽ gặp lỗi IndexError hoặc KeyError hoặc ValueError xấu xí làm gián đoạn quy trình

Điều gì sẽ xảy ra nếu bạn không muốn gặp lỗi chỉ vì bạn không thể tìm thấy giá trị?

Đây là một chức năng đơn giản để xử lý tất cả các tình huống này. Nếu có bất kỳ dạng lỗi nào, hàm sẽ trả về giá trị Không có hoặc tùy chỉnh chỉ định giá trị mặc định tùy chỉnh

giám đốc

Bây giờ hãy thử lấy một giá trị từ Dict mẫu ở trên,

age_minimum = dictor(data, "properties.age.minimum")

để có được một giá trị chỉ mục cụ thể,

# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball

Đây là khóa giả không tồn tại, giá trị được trả về là Không có, không có lỗi Khóa hoặc Chỉ mục hoặc Giá trị lộn xộn,

fake_val = dictor(data, "interests.blah.3.shmaaa")
>> None

Nếu bạn cần chuyển một tham số vào đường dẫn tra cứu của mình, chỉ cần sử dụng định dạng,

lookup = "interests"
value = dictor(data, "{}".format(lookup))
>> {u'sports': [u'hockey', u'baseball', u'archery']}

nếu không có giá trị hoặc không có khóa hoặc một số loại lỗi tra cứu, để trả về giá trị dự phòng tùy chỉnh thay vì Không có, hãy sử dụng giá trị mặc định

dictor(data, "properties.occupation.8", default="Missing Occupation value")
>> Missing Occupation value

Nếu toàn bộ tệp JSON là một danh sách, tức là,

[
{
"color": "red",
"value": "#f00"
},
{
"color": "green",
"value": "#0f0"
},
{
"color": "blue",
"value": "#00f"
}
]

để tra cứu giá trị Hex cho Blue, hãy sử dụng giá trị chỉ mục làm đối số thứ nhất

import json
from dictor import dictor
with open(‘sample.json’) as data:
data = json.load(data)

name = dictor(data, 'properties.users.3.name')
0

để tra cứu Danh sách Danh sách lồng nhau, ví dụ:

import json
from dictor import dictor
with open(‘sample.json’) as data:
data = json.load(data)

name = dictor(data, 'properties.users.3.name')
1

để lấy giá trị Màu,

import json
from dictor import dictor
with open(‘sample.json’) as data:
data = json.load(data)

name = dictor(data, 'properties.users.3.name')
2

cuối cùng, nếu bạn muốn Dictor đưa ra một ngoại lệ nếu giá trị trả về là KHÔNG, hãy cung cấp cờ, “checknone=True”

Nếu giá trị được kéo là Không, nó sẽ tăng ValueError

Đây là một cách hay để kiểm tra các biến đã cấu hình của bạn và loại bỏ lỗi quy trình nếu tham số cấu hình thiếu một giá trị

import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
11
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
17
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
31
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
32
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
12
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
34
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
35
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
36
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
37
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
38
import jsonwith open(‘sample.json’) as data: 
data = json.load(data)
print(data['properties']['age'])
39
age_minimum = dictor(data, "properties.age.minimum")
20

 

age_minimum = dictor(data, "properties.age.minimum")
21

age_minimum = dictor(data, "properties.age.minimum")
22
age_minimum = dictor(data, "properties.age.minimum")
23
age_minimum = dictor(data, "properties.age.minimum")
24
age_minimum = dictor(data, "properties.age.minimum")
25
age_minimum = dictor(data, "properties.age.minimum")
26
age_minimum = dictor(data, "properties.age.minimum")
27______226
age_minimum = dictor(data, "properties.age.minimum")
29
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
20
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
21
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
22
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
23
age_minimum = dictor(data, "properties.age.minimum")
26
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
25
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
20
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
27
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
28
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
29
# gets Sports 
sports = dictor(data, "interests.sports.1")
>> baseball
20
fake_val = dictor(data, "interests.blah.3.shmaaa")
>> None
31
fake_val = dictor(data, "interests.blah.3.shmaaa")
>> None
32