gpt4 book ai didi

python-2.7 - 如何使用机器人框架遍历 JSON 数组

转载 作者:行者123 更新时间:2023-12-04 01:14:18 30 4
gpt4 key购买 nike

我想遍历 json 响应并获取 id 和 name 元素数据。我正在尝试的响应 json 和代码如下。谁能让我知道我在做什么错误。

[
{
"id": 3,
"policyRevId": 3,
"busId": "POL_0003",
"revision": 1,
"name": "CIS Microsoft Windows Server 2012 R2 v2.2.1"
},
{
"id": 2,
"policyRevId": 2,
"busId": "POL_0002",
"revision": 1,
"name": "CIS SUSE Linux Enterprise Server 11 Benchmark v 2.0"

},
{
"id": 1,
"policyRevId": 1,
"busId": "POL_0001",
"revision": 1,
"name": "Payment Card Industry (PCI) Data Security Standard version 3.1"
}
]


*** Settings ***
Library RequestsLibrary
Library Collections
Library strings


*** Test case ***

Get Policy With Auth
@{auth}= Create List user pass
Create Session httpbin https://test123.com/ auth=@{auth}
${resp}= Get Request httpbin uri=/policies/
:FOR ${i} in ${resp.json()}
\ Log ${i}
\ ${get_policy_id}= Get Variable Value ${resp.json()[i]['id']}
\ ${policy_name}= Get Variable Value ${resp.json()[i]['name']}
\ Log ${policy_id},${policy_name}

最佳答案

${resp.json()} 中的数据是一个列表,因此您应该能够直接迭代列表中的项目。请注意,在以下示例中,@ 用于 :FOR 语句,并且 ${item} 是字典而不是索引:

:FOR   ${item}   in  @{resp.json()}
\ Log ${item}
\ ${get_policy_id}= Set variable ${item['id']}
\ ${policy_name}= Set variable ${item['name']}
\ Log ${policy_id},${policy_name}

关于python-2.7 - 如何使用机器人框架遍历 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42865900/

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