机器人框架从 JSON 列表中获取 [英] Robot framework get from JSON list
问题描述
我有这样的数据
{"_embedded":[
{"attr_name":"attr_value_1"},
{"attr_name":"attr_name_2"}
]
}
我想获得第一个属性 attr_value_1
.
and I want to get the first attribute attr_value_1
.
你知道如何在 Robot 框架中做到这一点的好方法吗?类似于 Get JSON/_embedded[0]/attr_name
?
Do you know any nice way how to do it in Robot framework? Something like Get JSON /_embedded[0]/attr_name
?
推荐答案
可以将JSON数据转换成python对象,然后使用robot的扩展变量语法 获取元素.在python中,该值被引用为data["_embedded"][0]["attr_name"]
,它直接转换为${data["_embedded"][的机器人扩展变量语法0]["attr_name"]}
You can convert the JSON data to a python object, and then use robot's extended variable syntax to get at the element. In python the value is referenced as data["_embedded"][0]["attr_name"]
, which translates directly to robot extended variable syntax of ${data["_embedded"][0]["attr_name"]}
这是一个完整的例子:
*** Test Cases ***
Example
${json}= catenate SEPARATOR=\n
... {"_embedded":[
... {"attr_name":"attr_value_1"},
... {"attr_name":"attr_name_2"}
... ]
... }
${data}= evaluate json.loads('''${json}''') json
should be equal as strings ${data["_embedded"][0]["attr_name"]} attr_value_1
should be equal as strings ${data["_embedded"][1]["attr_name"]} attr_name_2
这篇关于机器人框架从 JSON 列表中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!