RobotFramework:如何获取JSON的所有键作为列表

时间:2018-07-10 14:50:01

标签: python-3.x api automation robotframework

有人可以解释一下如何在机器人框架中将JSON的所有键作为列表获取吗?

示例:

{
   "api": "rest",
   "framework": "robot-framework"
}

我必须获取属性列表(api,框架)

2 个答案:

答案 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