gpt4 book ai didi

python - 在 python 中循环和计算 json 响应

转载 作者:行者123 更新时间:2023-11-28 22:46:53 26 4
gpt4 key购买 nike

我有以下 json 响应:

{

"routers":[
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.21"
}
]
},
"name":"test_router3",
"admin_state_up":true,
"tenant_id":"f19408f548234b71a5052549cb89ae83",
"distributed":false,
"routes":{
},
"ha":false,
"id":"102fc36a-e2f7-427f-983d-f71b77be0beb"
},
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.20"
}
]
},
"name":"test_router2",
"admin_state_up":true,
"tenant_id":"c3a69d0bbc944b1fbb0873efd71189db",
"distributed":false,
"routes":{
},
"ha":false,
"id":"11b413a3-975e-491c-b0cf-9e2ab11a17c4"
},
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.18"
}
]
},
"name":"test_router0",
"admin_state_up":true,
"tenant_id":"82c3cbe17d8e431a945ee972067b4063",
"distributed":false,
"routes":{
},
"ha":false,
"id":"17ed98c2-36d3-49ed-b9bb-a51616ae0112"
},
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.19"
}
]
},
"name":"test_router1",
"admin_state_up":true,
"tenant_id":"9930b010b96b44c8844dcac7e5b15682",
"distributed":false,
"routes":{
},
"ha":false,
"id":"68135a8f-a28c-43c3-9609-8a23b7b7452e"
},
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.22"
}
]
},
"name":"test_router4",
"admin_state_up":true,
"tenant_id":"f844661b3a7d413a8d66a12e77833539",
"distributed":false,
"routes":{
},
"ha":false,
"id":"6bbb58df-2699-4428-a95e-de51ed4cb37b"
},
{
"status":"ACTIVE",
"external_gateway_info":{
"network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
"enable_snat":true,
"external_fixed_ips":[
{
"subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
"ip_address":"194.168.0.3"
}
]
},
"name":"external_router1",
"admin_state_up":true,
"tenant_id":"c3a69d0bbc944b1fbb0873efd71189db",
"distributed":false,
"routes":{
},
"ha":false,
"id":"dd7a4123-4b5a-49df-bc42-db71772cffd0"
}
]

我需要得到以下信息:

  1. 整个响应中“id”的计数

谁能告诉我如何遍历 json。

我尝试了 for 循环,但出现错误“将整数作为索引执行”

最佳答案

这里有一段代码可以计算路由项“id”属性的出现次数

>>> import json
>>> json_data = json.loads(raw_json_data)
>>> id_count = sum(1 for route in json_data['routers'] if route.get('id'))
>>>
>>> id_count
6

关于python - 在 python 中循环和计算 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26880877/

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