Ton*_*ana 0 python json robotframework
作为Robot Framework验证的一部分,我将以下数据(存储为${response})作为获取请求响应:
{
"interfaces": [
{
"name": "eth0",
"status": "ready",
"macAddress": "xx:xx:xx:xx:xx:xx",
"ipv4": {
"mode": "DHCP",
"address": "127.0.0.1",
"mask": "255.255.255.0",
},
"ipv6": {
"mode": "DISABLED",
"addresses": [],
"gateway": "",
}
}
],
"result": 0
}
Run Code Online (Sandbox Code Playgroud)
我想获取键的值ipv4并将其与预定义值进行比较。我尝试使用它,HttpLibrary.HTTP因为Robot Framework 3.1将不推荐使用它,因此我想使用Evaluate。机器人框架中是否可能?
如果变量${response}是响应对象(而不是字符串),则是有效载荷的内容,最直接的方法是调用其json()方法,该方法将有效载荷作为已解析的字典返回:
${the data}= Evaluate ${response.json()}
Run Code Online (Sandbox Code Playgroud)
另一种方法是自己解析有效负载json.loads(),并传递.content存储它的属性(这几乎.json()是内部执行的操作):
${the data}= Evaluate json.loads(${response.content}) json
Run Code Online (Sandbox Code Playgroud)
如果该变量${response}是字符串,即实际有效载荷,则将其传递给json.loads():
${the data}= Evaluate json.loads($response) json
Run Code Online (Sandbox Code Playgroud)
现在您已将数据作为常规字典保存,请按常规方式进行验证:
Should Be Equal ${the data['interfaces'][0]['ipv4']} ${your predefined dictionary}
Run Code Online (Sandbox Code Playgroud)