代码之家  ›  专栏  ›  技术社区  ›  JD2775

在Python中迭代JSON文件

  •  0
  • JD2775  · 技术社区  · 7 年前

    {
        "firstName": "Jane",
        "lastName": "Doe",
        "hobbies": ["running", "sky diving", "singing"],
        "age": 35,
        "children": [
            {
                "firstName": "Alice",
                "age": 6
            },
            {
                "firstName": "Bob",
                "age": 8
            }
        ]
    },
    {
        "firstName": "Mike",
        "lastName": "Smith",
        "hobbies": ["bowling", "photography", "biking"],
        "age":40,
        "children": [
            {
                "firstName": "Steve",
                "age": 10
            },
            {
                "firstName": "Sara",
                "age": 18
            }
        ]
    }
    

    我使用以下代码加载json文件:

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

    我可以像这样打印第一张唱片的一部分:

    print(data['children'])
    print(data['hobbies'])
    
    [{'firstName': 'Alice', 'age': 6}, {'firstName': 'Bob', 'age': 8}]
    ['running', 'sky diving', 'singing']
    

    不过,我想反复浏览这些记录,这样我也可以打印第二个条目(或者第三个条目,或者第20个条目,如果适用的话)

    但是,当我尝试这样做时:

    for key, value in data.items():
        print(key, value)
    

    它仍然只是返回第一个条目:

    firstName Jane
    lastName Doe
    hobbies ['running', 'sky diving', 'singing']
    age 35
    children [{'firstName': 'Alice', 'age': 6}, {'firstName': 'Bob', 'age': 8}]
    

    有什么想法吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Arghya Saha    7 年前

    您面临的问题是,您将数据作为单个json。

    你需要把它做成一个数组。像这样的。

    [{  // notice additional [
        "firstName": "Jane",
        "lastName": "Doe",
        "hobbies": ["running", "sky diving", "singing"],
        "age": 35,
        "children": [
            {
                "firstName": "Alice",
                "age": 6
            },
            {
                "firstName": "Bob",
                "age": 8
            }
        ]
    },
    {
        "firstName": "Mike",
        "lastName": "Smith",
        "hobbies": ["bowling", "photography", "biking"],
        "age":40,
        "children": [
            {
                "firstName": "Steve",
                "age": 10
            },
            {
                "firstName": "Sara",
                "age": 18
            }
        ]
    }]  // notice additional ]
    

    import json
    
    with open('abc.json') as f:
        data = json.load(f)
    
    for element in data:
        for key, value in element.items():
            print(key, value)
    
        2
  •  1
  •   Frank AK    7 年前

    把你的档案藏起来会更危险 JSON ,然后打开它 [ ]

    import json
    
    f = open("test_json.txt", "r")
    contents = f.readlines()
    f.close()
    
    
    contents.insert(0, "[")
    
    f = open("test_json.txt", "w")
    contents = "".join(contents)
    f.write(contents)
    f.write("]")
    f.close()
    
    
    with open("test_json.txt", "r") as fd:
        d = json.load(fd)
        for i in d:
            print(i)