有人可以解释一下如何在机器人框架中将JSON的所有键作为列表获取吗?
示例:
{
"api": "rest",
"framework": "robot-framework"
}
我必须获取属性列表(api,框架)
答案 0 :(得分:0)
您需要将JSON字符串转换为字典,然后在字典上调用keys
方法。对于后者,您可以使用内置关键字call method,也可以使用extended variable syntax。
示例:
*** Variables ***
${json_string}
... {
... "api": "rest",
... "framework": "robot-framework"
... }
*** Test cases ***
Example
# convert the JSON to a python object
${json}= evaluate json.loads($json_string) json
# get the keys using `call method`
${keys}= call method ${json} keys
should contain ${keys} api
should contain ${keys} framework
# get the keys using extended variable syntax
${keys}= set variable ${json.keys()}
should contain ${keys} api
should contain ${keys} framework
答案 1 :(得分:0)
或者,我使用的Collections库中有一个Get Dictionary Keys关键字。它完全符合您的要求:
${json} = Convert To Dictionary ${json}
${list} = Get Dictionary Keys ${json}
Log ${list} #This should now give you api and framework