添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

从Json文件中获取子节点数据

0 人关注

我从JSON文件中得到了一个列表,但我怎样才能用Python从每个结果中得到用户名和结果?

Json File

"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"

The result should be:

username = abcde, halo, 01
result = Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
    
2 个评论
到目前为止,你都试过什么?
看一下python的内置 json module
python
list
nodechildren
JsBoon
JsBoon
发布于 2021-03-10
2 个回答
Nima
Nima
发布于 2021-03-10
已采纳
0 人赞同
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))

Result

username :  abcde, halo, 01
result :  Back Road Explorer, Outdoor Adventure, Culinary Connoisseur
    
David Meu
David Meu
发布于 2021-03-10
0 人赞同

可能你需要这样的东西。

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"])