无法在 Robotframework 中使用点符号访问字典

Can't access dictionary with dot notation in Robotframework

我有这样的嵌入式词典:

${json}=    Create Dictionary ......(value pairs)
${request}= Create Dictionary   body=${json}
${return}=  Create Dictionary   request=${request}

我可以像这样访问原始键值对:

${return.request.body.type}

一切正常。但是,我收到此错误:

Resolving variable '${detail_call.response.json.type}' failed: AttributeError: 'dict' object has no attribute 'type'

当我尝试创建初始 json 对象时,反之亦然 - 通过从字符串解码它,如下所示:

${decoded_json}=    Run Keyword And Ignore Error    json.JSONDecoder.Decode ${response.content}
${response.json}=   Set Variable If '${decode_status}' == 'PASS'    ${decoded_json} ${null}
${detail_call}=    Create Dictionary    response=${response}

并通过点符号访问它:

${detail_call.response.json.type}

当我记录字符串时,我可以清楚地看到有键 "type" 并分配了值。当我用括号访问它时它甚至可以工作:

${detail_call.response.json['type']}

如果字典是用 JSONDecoder 创建的,你知道为什么我不能使用点符号吗?

谢谢。

正如 millimoose 所建议的,JSONDecoder returns python 字典不同于 robotframework 自己的字典。我还没有找到将 python dict 转换为机器人 dict 的官方方法,所以我实现了自己的:

Convert Python Dictionary
[Arguments]    ${python_dict}
[Documentation]    Converts Python dictionary to Robot dictionary.
@{keys}=    Get Dictionary Keys    ${python_dict}
${robot_dict}=    Create Dictionary
:FOR    ${key}    IN    @{keys}
\    Set To Dictionary    ${robot_dict}    ${key}=${python_dict['${key}']}
[Return]    ${robot_dict}

如果您需要将机器人字典转换为 python 字典,您可以使用 Collections 库中的 Convert To Dictionary 关键字。