Python 在Robot框架中将列表转换为Json
调用specificPython 在Robot框架中将列表转换为Json,python,json,robotframework,Python,Json,Robotframework,调用specificGet 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
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
然后我得到错误解析变量“${response[0]['result']}”失败:TypeError:字符串索引必须是整数
根据文档(Robot框架的集合库),Get Regexp Matches
始终返回列表。
我想验证这些值是否匹配(result=1,namespace=global,reason=unauthorized),但处理不起作用。
谢谢 谢谢@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
看起来
response
是一个包含单个字符串的列表。您应该使用resp0=json.loads(response[0])
然后使用resp0['result']
等等。这是否回答了您的问题?