从 Json 文件中获取子节点数据
Get the child node data from Json File
我从 JSON 文件中得到了一个列表,但是如何使用 Python 从每个结果中获取用户名和结果?
Json 文件
{
"Users" : {
"abcde" : {
"email" : "123@gmail.com",
"gender" : "Male",
"password" : "123",
"result" : "Back Road Explorer",
"username" : "abcde"
},
"halo" : {
"email" : "halo@gmail.com",
"password" : "halo",
"result" : "Outdoor Adventure",
"username" : "halo"
},
"01" : {
"email" : "01@gmail.com",
"gender" : "Male",
"password" : "dajcaq",
"result" : "Culinary Connoisseur",
"username" : "01"
}
}
结果应该是:
username = abcde, halo, 01
result = Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
json = {...}
result_username = []
result = []
for username in json['Users']:
result_username += [username]
result += [json['Users'][username]['result']]
print ('username : ',', '.join(result_username))
print ('result : ',', '.join(result))
结果
username : abcde, halo, 01
result : Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
可能你需要这样的东西:
js = {
"Users": {
"abcde": {
"email": "123@gmail.com",
"gender": "Male",
"password": "123",
"result": "Back Road Explorer",
"username": "abcde"
},
"halo": {
"email": "halo@gmail.com",
"password": "halo",
"result": "Outdoor Adventure",
"username": "halo"
},
"01": {
"email": "01@gmail.com",
"gender": "Male",
"password": "dajcaq",
"result": "Culinary Connoisseur",
"username": "01"
}
}
}
def parse_json(js):
usernames, results = [], []
for value in js['Users'].values():
usernames.append(value["username"])
results.append(value["result"])
usernames, results = ', '.join(usernames), ', '.join(results),
print(f'username = {usernames}')
print(f'result = {results}')
我从 JSON 文件中得到了一个列表,但是如何使用 Python 从每个结果中获取用户名和结果?
Json 文件
{
"Users" : {
"abcde" : {
"email" : "123@gmail.com",
"gender" : "Male",
"password" : "123",
"result" : "Back Road Explorer",
"username" : "abcde"
},
"halo" : {
"email" : "halo@gmail.com",
"password" : "halo",
"result" : "Outdoor Adventure",
"username" : "halo"
},
"01" : {
"email" : "01@gmail.com",
"gender" : "Male",
"password" : "dajcaq",
"result" : "Culinary Connoisseur",
"username" : "01"
}
}
结果应该是:
username = abcde, halo, 01
result = Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
json = {...}
result_username = []
result = []
for username in json['Users']:
result_username += [username]
result += [json['Users'][username]['result']]
print ('username : ',', '.join(result_username))
print ('result : ',', '.join(result))
结果
username : abcde, halo, 01
result : Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
可能你需要这样的东西:
js = {
"Users": {
"abcde": {
"email": "123@gmail.com",
"gender": "Male",
"password": "123",
"result": "Back Road Explorer",
"username": "abcde"
},
"halo": {
"email": "halo@gmail.com",
"password": "halo",
"result": "Outdoor Adventure",
"username": "halo"
},
"01": {
"email": "01@gmail.com",
"gender": "Male",
"password": "dajcaq",
"result": "Culinary Connoisseur",
"username": "01"
}
}
}
def parse_json(js):
usernames, results = [], []
for value in js['Users'].values():
usernames.append(value["username"])
results.append(value["result"])
usernames, results = ', '.join(usernames), ', '.join(results),
print(f'username = {usernames}')
print(f'result = {results}')