如何在robot框架中使用json中的另一个值提取一个值
作为Robot框架验证的一部分,我将以下数据(存储为${resp})作为get请求响应:如何在robot框架中使用json中的另一个值提取一个值,json,python-3.x,robotframework,Json,Python 3.x,Robotframework,作为Robot框架验证的一部分,我将以下数据(存储为${resp})作为get请求响应: { "numberOfElements": 2, "data": [ { "id": "1", "accId": "123456789", "total": 13, "isEnabled": false }, { "id": "2", "accId":
{
"numberOfElements": 2,
"data": [
{
"id": "1",
"accId": "123456789",
"total": 13,
"isEnabled": false
},
{
"id": "2",
"accId": "987654321",
"total": 52,
"isEnabled": false
}
],
"last": true
}
从上面的json中,我想提取accId为123456789的id。如何使用条件参数使用json中的任何其他值搜索值。
我已经实现了这个验证numberfelemets的代码
${json_resp} = Set Variable ${resp.json()}
${numberOfElements} set variable ${json_resp['numberOfElements']}
should be equal as numbers ${numberOfElements} 2
简单的步骤是尝试将json转换为字典和循环思想字典
${json_resp} = Set Variable ${resp.json()}
${json}= evaluate json.loads('''${json_resp}''') json
:FOR ${i} IN @{json['data']}
\ log to Console ${i['accId']}
然后,您应该可以访问
accId
获得了以下解决此问题的方法,该方法对我有效:
${uuid}= set variable
:FOR ${i} IN @{json_resp['data']}
\ ${uuid}= run keyword if '${i['id']}'=='123456789' get uuid with id ${i}
log to console ${uuid}
修正码
“退出循环”帮助不将${id}重写为无
*** Test Cases ***
Test1
:FOR ${i} IN @{json_resp()['data']}
\ ${id}= run keyword if '${i['accId']}'=='123456789' Get Id ${i}
\ run keyword if '${i['accId']}'=='123456789' Exit For Loop
log to console ${id}
*** Keywords ***
Get Id
[Arguments] ${content_body}
${idf}= Get From Dictionary ${content_body} id
[Return] ${idf}
您已经尝试了什么?您观察到了什么?正如@A.Kootstra所提到的,只要您展示您已经尝试了什么以及您如何未能实现目标,人们都愿意帮助解决堆栈溢出问题。这有助于理解您真正想要做的事情。编辑为“我已经尝试过…”。。。。请现在检查这个示例代码是否能像您预期的那样工作?最初我使用这种方法来验证numberOfElements。但是现在我想验证与特定accountId相关的id。这就是我需要您帮助的原因。尝试了此方法后,我收到了此错误,因为响应json包含单引号失败:JSONDecodeError:期望属性名称包含在双引号中:第1行第2列(字符1)您收到的实际请求是什么?
*** Test Cases ***
Test1
:FOR ${i} IN @{json_resp()['data']}
\ ${id}= run keyword if '${i['accId']}'=='123456789' Get Id ${i}
\ run keyword if '${i['accId']}'=='123456789' Exit For Loop
log to console ${id}
*** Keywords ***
Get Id
[Arguments] ${content_body}
${idf}= Get From Dictionary ${content_body} id
[Return] ${idf}