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

时间:2015-01-31 22:53:29

标签: python json

如果我有一个如下所示的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中的列表。所有帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

使用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()