Python:循环JSON-Fi

2024-06-01 06:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何遍历这个顶级键A1和B6是随机的特殊结构。

我希望能够输出名称,x,y和yr

{
    "A1": {
        "msgs": ["Something there"],
        "name": "Mary White",
        "x": 132,
        "y": 73,
        "yr": 1978
    },
    "B6": {
        "msgs": ["Something here"],
        "name": "Joe Bloggs",
        "x": 132,
        "y": 73,
        "yr": 2016
    },
...

使用以下命令加载我的JSON

import json

with open('items.json') as data_file:    
    data = json.load(data_file)

Tags: name名称jsondataa1结构顶级something
1条回答
网友
1楼 · 发布于 2024-06-01 06:28:31

遍历.values()

import json

with open('data.json') as data_file:    
    data = json.load(data_file)
    for v in data.values():
        print(v['x'], v['y'], v['yr'])

相关问题 更多 >