Python 如何在JSON对象中搜索特定的期望键值列表?
我有一个json对象,我很好奇如何遍历servicecatalog:name并提醒任何不等于“service foo”或“service bar”的名称 这是我的json对象:Python 如何在JSON对象中搜索特定的期望键值列表?,python,list,Python,List,我有一个json对象,我很好奇如何遍历servicecatalog:name并提醒任何不等于“service foo”或“service bar”的名称 这是我的json对象: { "access": { "serviceCatalog": [ { "endpoints": [ { "internalURL": "https://
{
"access": {
"serviceCatalog": [
{
"endpoints": [
{
"internalURL": "https://snet-storage101.example.com//v1.0",
"publicURL": "https://storage101.example.com//v1.0",
"region": "LON",
"tenantId": "1
},
{
"internalURL": "https://snet-storage101.example.com//v1.0",
"publicURL": "https://storage101.example.com//v1.0",
"region": "USA",
"tenantId": "1
}
],
"name": "service-foo",
"type": "object-store"
},
{
"endpoints": [
{
"publicURL": "https://x.example.com:9384/v1.0/x",
"tenantId": "6y5t4re32"
}
],
"name": "service-bar",
"type": "rax:test"
},
{
"endpoints": [
{
"publicURL": "https://y.example.com:9384/v1.0/x",
"tenantId": "765432"
}
],
"name": "service-thesystem",
"type": "rax:test"
}
]
}
如果x是上述字典。你可以
for item in x["access"]["serviceCatalog"]:
if item["name"] not in ["service-foo", "service-bar"]:
print(item["name"])
ps:如果需要,可以使用json.loads()对json数据进行解码。您的JSON中也有错误。您的JSON
“tenantId”中有错误:“1
在结尾处缺少”
,在结尾处缺少
。