gpt4 book ai didi

python - 用Python解析JSON,数组数组的麻烦

转载 作者:行者123 更新时间:2023-12-01 00:33:19 25 4
gpt4 key购买 nike

我有一个如下所示的 JSON 结构

{
"PersonInformation": {
"PhysicalStatus": "",
"OpenDetainers": [],
"StartDate": "",
"FacilityLog": [],
"CustStatus": "",
"EndDate": ""
},
"IdentityList": [
{
"CreationDate": "01/01/1999",
"PersonNames": [
{
"Suffix": "",
"FirstName": "Johnny",
"LastName": "Appleseed",
"MiddleName": ""
},
{
"Suffix": "",
"FirstName": "Foo",
"LastName": "Bar",
"MiddleName": ""
}
],
"PlaceOfBirthList": [
{
"City": "Boston",
"State": "MA",
"CountryCode": ""
}
]
}
]
}

我可以像这样解析外部数组,但我无法弄清楚如何循环遍历子数组之一,例如“PersonNames”

所以我可以做到这一点

myjson = json.loads(json_data)
print myjson['PersonInformation']['PhysicalStatus']
for identity_list in myjson['IdentityList']:
print identity_list['CreationDate']

返回结果

OK
01/01/1999

如预期,但我不知道如何将其提升到下一个级别来遍历并循环“PersonNames”

感谢您的帮助

最佳答案

您可以像这样遍历 PersonNames 键下的子列表:

for identity in myjson['IdentityList']:
for person in identity['PersonNames']:
print person['FirstName'], person['LastName']

关于python - 用Python解析JSON,数组数组的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58016183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com