2015-01-31 6 views
-1

나는이처럼 보이는 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] 

값/키의 수는 객체마다 다르므로 나는 파이썬으로 목록에 올바르게 파싱하는 방법에 대해 조금 궁금해합니다. 모든 도움을 주셨습니다.

+0

중첩 된 사전으로 유지하는 것이 잘못된 이유는 무엇입니까? –

+0

좋아, IronPython 2.7을 기반으로 조금 이상하게도 이상하지 않으며 인쇄를 시도 할 때 완전한 사전을 지원하지 않는다. 목록으로 항목을 변환하기 위해 몇 가지 기본 사전 작업을 수행 할 수 있다고 생각하지만 사전. – konrad

답변

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)을 반복해야합니다. 각 하위 사전은 최상위 수준 사전의 값이므로 반복 할 때 을 호출하면됩니다.