python解析json数据

python解析json数据

demo

data = {
    "name": "John",
    "age": 30,
    "hobbies": ["reading", "running", "swimming"],
    "scores": [85, 90, 95]
}

for key, value in data.items():
    if isinstance(value, list):
        # 当值为列表类型时
        print(f"{key} is a list:")
        for item in value:
            print(item)
    else:
        # 当值不是列表类型时
        print(f"{key}: {value}")

读取json文件

# 从JSON文件中读取数据
    with open('example_json.json', 'r', encoding='utf-8') as file:
        data = json.load(file)
    res_list = []
    res = data["xxx"]
    for key, value in res.items():
        print(key)
    
posted @ 2023-07-05 09:53  靠谱杨  阅读(31)  评论(0编辑  收藏  举报