在python中将json文件解析为列表

2024-04-18 18:35:50 发布

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

如果我有一个json文件如下所示:

{
"Wall-2647b42e-611f-407a-a957-585c0eb63599-0007229d": {
    "wall_type": "CONCEPT 300mm RED",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-0007229d",
    "thickness": 0.9842,
    "material": "ACU-101"
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-00072309": {
    "wall_type": "CONCEPT 200mm",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072309",
    "thickness": 0.6561,
    "material": null
},
"Column-fedcf906-a98c-4c8e-acef-18540a1ee941-00072db3": {
    "level_name": "Ground",
    "section": "305x305x97UC",
    "material": null,
    "revit_id": "fedcf906-a98c-4c8e-acef-18540a1ee941-00072db3"
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-00072266": {
    "wall_type": "CONCEPT 300mm RED",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072266",
    "thickness": 0.9842,
    "material": "ACU-101"
},
"Column-fedcf906-a98c-4c8e-acef-18540a1ee941-00072ded": {
    "level_name": "Ground",
    "section": "305x305x97UC",
    "material": null,
    "revit_id": "fedcf906-a98c-4c8e-acef-18540a1ee941-00072ded"
},
"Floor-fedcf906-a98c-4c8e-acef-18540a1ee941-00072e79": {
    "floor_type": "Concrete 200 mm",
    "level_name": "Ground",
    "revit_id": "fedcf906-a98c-4c8e-acef-18540a1ee941-00072e79",
    "thickness": 0.2,
    "material": null
},
"Column-fedcf906-a98c-4c8e-acef-18540a1ee941-00072e2e": {
    "level_name": "Ground",
    "section": "305x305x97UC",
    "material": null,
    "revit_id": "fedcf906-a98c-4c8e-acef-18540a1ee941-00072e2e"
},
"Room-2647b42e-611f-407a-a957-585c0eb63599-0007270f": {
    "number": "A4001",
    "occupancy": "20",
    "name": "Conference Room",
    "floor_finish": "PT-01",
    "wall_finish": "STL-01",
    "height": 4,
    "level_name": "Ground",
    "ceiling_finish": "PT-01",
    "base_finish": "CPT-01",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-0007270f",
    "area": 65.002
},
"Room-2647b42e-611f-407a-a957-585c0eb63599-00072712": {
    "number": "A4002",
    "occupancy": "2",
    "name": "Office 1",
    "floor_finish": "PT-01",
    "wall_finish": "STL-01",
    "height": 4,
    "level_name": "Ground",
    "ceiling_finish": "PT-01",
    "base_finish": "CPT-01",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072712",
    "area": 37.45
},
"Floor-d2390d2c-7f71-414d-b143-e7f7d8048330-00072fa7": {
    "floor_type": "Concrete 200 mm",
    "level_name": "Level 1",
    "revit_id": "d2390d2c-7f71-414d-b143-e7f7d8048330-00072fa7",
    "thickness": 0.2,
    "material": null
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-00072a14": {
    "wall_type": "CONCEPT 200mm",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072a14",
    "thickness": 0.6561,
    "material": null
},
"Room-2647b42e-611f-407a-a957-585c0eb63599-00072a1f": {
    "number": "A4004",
    "occupancy": "1",
    "name": "Storage",
    "floor_finish": "None",
    "wall_finish": "None",
    "height": 4,
    "level_name": "Ground",
    "ceiling_finish": "None",
    "base_finish": "None",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072a1f",
    "area": 9.897
},
"Room-2647b42e-611f-407a-a957-585c0eb63599-00072715": {
    "number": "A4003",
    "occupancy": "2",
    "name": "Office 2",
    "floor_finish": "PT-01",
    "wall_finish": "STL-01",
    "height": 4,
    "level_name": "Ground",
    "ceiling_finish": "PT-01",
    "base_finish": "CPT-01",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072715",
    "area": 37.45
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-00072238": {
    "wall_type": "CONCEPT 300mm RED",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072238",
    "thickness": 0.9842,
    "material": "ACU-101"
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-000722be": {
    "wall_type": "CONCEPT 300mm RED",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-000722be",
    "thickness": 0.9842,
    "material": "ACU-101"
},
"Wall-2647b42e-611f-407a-a957-585c0eb63599-00072339": {
    "wall_type": "CONCEPT 200mm",
    "level_name": "Ground",
    "revit_id": "2647b42e-611f-407a-a957-585c0eb63599-00072339",
    "thickness": 0.6561,
    "material": null
},
"Column-fedcf906-a98c-4c8e-acef-18540a1ee941-00072e55": {
    "level_name": "Ground",
    "section": "305x305x97UC",
    "material": null,
    "revit_id": "fedcf906-a98c-4c8e-acef-18540a1ee941-00072e55"
}

}

如何将其解析为列表,其中:

objectLst = ['Floor-...', 'Room-...', 'Wall-...'] etc
keys = [['floor_type', 'level_name', 'revit_id', 'thickness', 'material' etc],['number', 'occupancy', 'floor_finish', 'wall_finish','name', 'occupancy' etc],['wall_type','level_name' etc]]
values = [all values from key/value relationship in json file like above keys]

每个对象的值/键的数量不同,因此我有点好奇如何在python中将其正确地解析为列表。感谢大家的帮助。你知道吗


Tags: nameidtypelevelnullmaterialwallthickness
1条回答
网友
1楼 · 发布于 2024-04-18 18:35:50

使用list comprehensions

data = json.load(data_file)

objects = data.keys()
keys = [d.keys() for sub_dict in data.values()]
values = [d.values() for sub_dict in data.values()]

对于objects,您只需要顶级字典中的键,因此非常简单。对于包含每个子字典中的键或值的列表,您需要遍历这些子字典(我称之为sub_dict)。每个子字典都是顶级字典的一个值,所以要遍历它们,只需调用data.values()。你知道吗

相关问题 更多 >