【问题标题】:Convert List to Json in Robot Framework在机器人框架中将列表转换为 Json
【发布时间】:2020-06-28 11:02:36
【问题描述】:

在调用特定的Get Regexp Matches 后,我得到了以下列表:

['{"result": 1, "error": { "namespace": "global", "reason": "unauthorized" } }']

当我通过以下方式验证值时:

Should Be Equal   ${response[0]['result']}  1
Should Be Equal   ${response[0]['error']['namespace']}  global
Should Be Equal   ${response[0]['error']['reason']}  unauthorized

然后我得到错误Resolving variable '${response[0]['result']}' failed: TypeError: string indices must be integers 基于文档(Robot Frameworks 的集合库),Get Regexp Matches 始终返回列表。 我想验证它们匹配的那些值(result=1,namespace=global,reason=unauthorized)但处理不起作用。 谢谢 !

【问题讨论】:

  • 看起来response 是一个包含单个字符串的列表。你应该使用resp0 = json.loads(response[0]),然后使用resp0['result']等等。
  • 这能回答你的问题吗? Parsing Json in Robot Framework

标签: python json robotframework


【解决方案1】:

感谢@quamrana。这样解决:

${response} =  Evaluate    json.loads('''${response[0]}''')    json
Should Be Equal   ${response['result']}  1
Should Be Equal   ${response['error']['namespace']}  global
Should Be Equal   ${response['error']['reason']}  unauthorized

【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多