【问题标题】:How can I parse json in python?如何在 python 中解析 json?
【发布时间】:2019-05-16 06:32:57
【问题描述】:

我有一个名为 Student_DB.json 的 JSON 文件。它包含与 JSON 格式的学生相关的所有信息。所以这是结构:

{
    "students" : [
         { "id": "1", "satisfactory": "true", "entered_class": "8:30 AM", "left_class": "9:50 AM" },
         { "id": 2, ...},
         { "id": 3, ...}
    ]
}

我想提取所有不同的字段并检查每个属性,即为所有学生创建一个 enter_class 时间数组并聚合它们。如何在python中实现这一点,请帮助。

【问题讨论】:

  • 我更新了我的帖子。现在这是 JSON。
  • 我的反对意见是因为您发布的内容显然不是 JSON,并且该问题显示出明显缺乏研究。连googled python json都不像你。

标签: python json


【解决方案1】:

更改您的 json 文件,如:

{
    "students" : [
         { "id": 1, "satisfactory": True, "entered_class": "8:30 AM", "left_class": "9:50 AM" },
         { "id": 2, ...},
         { "id": 3, ...}
    ]
}

从json文件中提取数据:

with open('Student_DB.json') as f:
    contents = json.load(f)

for data in contents['students']:
    print(data)
    print(data['id'])
    print(data['entered_class'])

O/P:

{'id': 1, 'satisfactory': True, 'entered_class': '8:30 AM', 'left_class': '9:50 AM'}
1
8:30 AM

【讨论】:

  • 这只是打印所有学生。我需要所有学生输入的类/数组中的任何子值。
  • 谢谢我得到了所有的行。我可以将它保存到数组中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多