python遍历json数组_在Python中使用Foreach循环从json响应中的数组中提取值

我有这个json响应:{

"properties": {

"basic": {

"bandwidth_class": "",

"failure_pool": "",

"max_connection_attempts": 0,

"max_idle_connections_pernode": 50,

"max_timed_out_connection_attempts": 2,

"monitors": [

"Simple HTTP"

],

"node_close_with_rst": false,

"node_connection_attempts": 3,

"node_delete_behavior": "immediate",

"node_drain_to_delete_timeout": 0,

"nodes_table": [

{

"node": "abc1.prod.local:80",

"priority": 1,

"state": "active",

"weight": 1

},

{

"node": "def1.prod.local:80",

"priority": 1,

"state": "disabled",

"weight": 1

},

{

"node": "ghi1.prod.local:80",

"priority": 1,

"state": "disabled",

"weight": 1

},

{

"node": "jkl1.prod.local:80",

"priority": 1,

"state": "active",

"weight": 1

}

],

"note": "",

"passive_monitoring": true,

"persistence_class": "",

"transparent": false

}

}

}

这个powershell脚本:$nodesAarray = "abc1.prod.local:80", "jkl1.prod.local:80"

foreach($node in $nodesArray)

{

$nodes_match_and_enabled = $GetNodesResponse.properties.basic.nodes_table | Where { $_.node -eq $node -and $_.state -eq "active" }

if($nodes_match_and_enabled)

{

Write-Output "$node exists in the pool and active"

}

else

{

Write-Output "$node is either not active or the name mismatches"

$global:invalidNodeArray.Add($node)

}

}

在我的powershell脚本中,我循环检查我的数组中的两个节点实际上是按值匹配,状态是否处于活动状态。它像我期望的那样工作。

但是,我在Python中编写了相同的逻辑(我是初学者),但不知道如何处理它。知道Python中的脚本会是什么样子???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值