解析 Robot 框架中的 JSON 输出 [尝试了论坛中的大多数选项]
Parsing JSON output in Robot framework [Tried most options in the forum]
我有这样的 JSON 输出:
{
":output":{
"response":"{ \"ParentId\" : 125, \"ParentKey\" : { \"key\" : \"9aqews-uwdguwdw8-9uw8\", \"identity\" : \"key_ID=674\" } }"
}
}
我正在尝试获取密钥的内容,即:9aqews-uwdguwdw8-9uw8
以下是尝试过的东西:
------------------------------------------------------
${json_data} Parse Json ${output}
Log ${json_data["output"]["response"]}
Log ${json_data["output"]["response"][0][0:10]}
------------------------------------------------------
${json}= Convert To Dictionary ${values}
${j_keys}= Get Dictionary Keys ${json}
Log ${j_keys}
------------------------------------------------------
${values}= Evaluate json.loads($output) json
Log ${values['output']['response'][1]}
-----------------------------------------------------
${KeySP}= Get Substring ${values} "key" ","
Log ${keySP}
------------------------------------------------------
#${parkeydict}= ${values['output']['response']}
#${keyspacedict}= ${parkeydict['ParentKey']}
#Log ${keyspacedict['key']}
------------------------------------------------------
我尝试了其他几个步骤、可能性和关键字,
我能解析的最好的是直到“Log ${json_data["output"]["response"]}" which returns data from 'response'.
即使我转换为 Dict 并访问 'key',它也会失败,我认为 'response' 之后的更多数据已完全存储为值。
有人可以 help/guide 告诉我如何捕获 'key' 变量中的数据吗?
提前致谢!
第一个问题是您尝试使用 ['output']
,但密钥是 :output
。
第二个问题是“response”键的值不是字典,它是一个看起来像字典的字符串。在尝试从中提取值之前,您必须将其转换为字典,假设它确实是一个格式正确的 json 字典,而不仅仅是一个看起来像字典的字符串。
这适用于我在问题中提供的确切数据:
${values}= Evaluate json.loads($output)
${response}= Evaluate json.loads($values[':output']['response'])
${key}= set variable ${response['ParentKey']['key']}
should be equal ${key} 9aqews-uwdguwdw8-9uw8
注意:如果您使用的机器人版本早于 3.2,您需要将 json
作为 Evaluate
命令的最后一个参数,以便机器人知道导入模块。从 3.2 版开始,表达式中使用的模块会自动导入。
我有这样的 JSON 输出:
{
":output":{
"response":"{ \"ParentId\" : 125, \"ParentKey\" : { \"key\" : \"9aqews-uwdguwdw8-9uw8\", \"identity\" : \"key_ID=674\" } }"
}
}
我正在尝试获取密钥的内容,即:9aqews-uwdguwdw8-9uw8
以下是尝试过的东西:
------------------------------------------------------
${json_data} Parse Json ${output}
Log ${json_data["output"]["response"]}
Log ${json_data["output"]["response"][0][0:10]}
------------------------------------------------------
${json}= Convert To Dictionary ${values}
${j_keys}= Get Dictionary Keys ${json}
Log ${j_keys}
------------------------------------------------------
${values}= Evaluate json.loads($output) json
Log ${values['output']['response'][1]}
-----------------------------------------------------
${KeySP}= Get Substring ${values} "key" ","
Log ${keySP}
------------------------------------------------------
#${parkeydict}= ${values['output']['response']}
#${keyspacedict}= ${parkeydict['ParentKey']}
#Log ${keyspacedict['key']}
------------------------------------------------------
我尝试了其他几个步骤、可能性和关键字, 我能解析的最好的是直到“Log ${json_data["output"]["response"]}" which returns data from 'response'.
即使我转换为 Dict 并访问 'key',它也会失败,我认为 'response' 之后的更多数据已完全存储为值。
有人可以 help/guide 告诉我如何捕获 'key' 变量中的数据吗?
提前致谢!
第一个问题是您尝试使用 ['output']
,但密钥是 :output
。
第二个问题是“response”键的值不是字典,它是一个看起来像字典的字符串。在尝试从中提取值之前,您必须将其转换为字典,假设它确实是一个格式正确的 json 字典,而不仅仅是一个看起来像字典的字符串。
这适用于我在问题中提供的确切数据:
${values}= Evaluate json.loads($output)
${response}= Evaluate json.loads($values[':output']['response'])
${key}= set variable ${response['ParentKey']['key']}
should be equal ${key} 9aqews-uwdguwdw8-9uw8
注意:如果您使用的机器人版本早于 3.2,您需要将 json
作为 Evaluate
命令的最后一个参数,以便机器人知道导入模块。从 3.2 版开始,表达式中使用的模块会自动导入。