Python 解析嵌套的 JSON 文件并取出需要的属性
Python parse nested JSON file and take out needed attributes
所以这是我的 JSON 文件:
data = {
"Module1": {
"Description": "",
"Layer": "1",
"SourceDir": "pathModule1",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "pathToCom1",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1:"{
...(something)
}
"sw_item2:"{
...(something)
}
"sw_item3:"{
...(something)
}
}
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "pathToCom2",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1_2:"{
...(something)
}
"sw_item2_2:"{
...(something)
}
"sw_item3_3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module2": {
"Description": "",
"Layer": "2",
"SourceDir": "pathModule2",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "pathToCom1",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1:"{
...(something)
}
"sw_item2:"{
...(something)
}
"sw_item3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "pathToCom2",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1_2:"{
...(something)
}
"sw_item2_2:"{
...(something)
}
"sw_item3_3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module3": {
"Description": "",
"Layer": "3",
"SourceDir": "path",
"Attributes": {
"some",
},
"Vendor": "",
},
"Module4": {
"Description": "",
"Layer": "4",
"SourceDir": "path",
"Attributes": {
"some",
}
}
}
我只需要将所有 sw_items.
的名称写入列表 (list_sw_items)
所以最后list_sw_items = [sw_item1, sw_item2, sw_item3, sw_item1_2, sw_item2_2, sw_item3_2, sw_item1, sw_item2, sw_item3]
我现在的代码,我在 sw_items
上不断收到错误 (KeyError)
for k,v in data.items():
for i,n in v['sw_items'].items():
有什么方法可以做到这一点?
谢谢!
for k,v in data.items():
for i,n in v['components'].items():
for j, l in n['sw_items'].items():
所以这是我的 JSON 文件:
data = {
"Module1": {
"Description": "",
"Layer": "1",
"SourceDir": "pathModule1",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "pathToCom1",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1:"{
...(something)
}
"sw_item2:"{
...(something)
}
"sw_item3:"{
...(something)
}
}
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "pathToCom2",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1_2:"{
...(something)
}
"sw_item2_2:"{
...(something)
}
"sw_item3_3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module2": {
"Description": "",
"Layer": "2",
"SourceDir": "pathModule2",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "pathToCom1",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1:"{
...(something)
}
"sw_item2:"{
...(something)
}
"sw_item3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "pathToCom2",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"sw_items:"{
"sw_item1_2:"{
...(something)
}
"sw_item2_2:"{
...(something)
}
"sw_item3_3:"{
...(something)
}
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module3": {
"Description": "",
"Layer": "3",
"SourceDir": "path",
"Attributes": {
"some",
},
"Vendor": "",
},
"Module4": {
"Description": "",
"Layer": "4",
"SourceDir": "path",
"Attributes": {
"some",
}
}
}
我只需要将所有 sw_items.
的名称写入列表 (list_sw_items)所以最后list_sw_items = [sw_item1, sw_item2, sw_item3, sw_item1_2, sw_item2_2, sw_item3_2, sw_item1, sw_item2, sw_item3]
我现在的代码,我在 sw_items
上不断收到错误 (KeyError)for k,v in data.items():
for i,n in v['sw_items'].items():
有什么方法可以做到这一点?
谢谢!
for k,v in data.items():
for i,n in v['components'].items():
for j, l in n['sw_items'].items():