在 ROBOT Framework 中获取列表中的变量
Fetching a variable inside a list in ROBOT Framework
假设下面是我的回复正文
body={
"message": {
"Code": 26181,
"rollnos": [
{
"ID": 1439173,
"date_input": "2022-01-31 14:09:30.206748",
}
],
"start_date": "2022-01-31 00:00:00",
}
}
我想在变量中设置 ID 值。
我已经尝试了以下方法,但没有成功。
${json_response}= set variable ${xyz.json()}
${temp}= Set Variable ${json_response['message']}
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
谁能帮我解决一下我哪里做错了。
嗯,当你说:
but it did not work out.
你没有提到错误信息是什么。
这是我对你的代码的解释:
${json_response}= set variable ${xyz.json()}
# ${json_response} is now a dictionary with content: body=&{message}
# So the next step would be:
${temp}= Set Variable ${json_response['body']['message']}
# And now we have: &{temp} = { Code:26181, &{rollnos}, ...
# The next code will be correct
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1['ID']
但是你得到了ID的值,你想设置它,所以这是另一个问题。
如果您已经有 body
作为字典,您只能使用测试示例中的最后 4 行。我还包括了从字符串到字典的转换,以备不时之需:
*** Settings ***
Library Collections
Resource robot/resources/environment_resources.robot
*** Variables ***
${body_temp} {"message": {"Code": "26181", "rollnos": [{"ID": "1439173", "date_input": "2022-01-31 14:09:30.206748"}],"start_date": "2022-01-31 00:00:00"}}
*** Keywords ***
Converting a JSON File
${body} evaluate json.loads($body_temp) json
[Return] ${body}
*** Test Cases ***
Example
# Get body as dict
${body}= converting a json file
# Get the ID
${message} = Get From Dictionary ${body} message
${rollnos} = Get From Dictionary ${message} rollnos
${id} = Get From Dictionary ${rollnos}[0] ID
# Log the ID
Log To Console ID:${id}
Can someone please help where I went wrong in this.
您将 ${temp['rollnos']}
视为字典,但它是一个字典列表。
而不是这个:
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
执行此操作,以获取字典中第一项的 ID:
${value_1}= Set Variable ${temp['rollnos'][0]}
# ^^^
${register_id} = Set Variable ${value_1["ID"]}
假设下面是我的回复正文
body={
"message": {
"Code": 26181,
"rollnos": [
{
"ID": 1439173,
"date_input": "2022-01-31 14:09:30.206748",
}
],
"start_date": "2022-01-31 00:00:00",
}
}
我想在变量中设置 ID 值。 我已经尝试了以下方法,但没有成功。
${json_response}= set variable ${xyz.json()}
${temp}= Set Variable ${json_response['message']}
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
谁能帮我解决一下我哪里做错了。
嗯,当你说:
but it did not work out.
你没有提到错误信息是什么。
这是我对你的代码的解释:
${json_response}= set variable ${xyz.json()}
# ${json_response} is now a dictionary with content: body=&{message}
# So the next step would be:
${temp}= Set Variable ${json_response['body']['message']}
# And now we have: &{temp} = { Code:26181, &{rollnos}, ...
# The next code will be correct
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1['ID']
但是你得到了ID的值,你想设置它,所以这是另一个问题。
如果您已经有 body
作为字典,您只能使用测试示例中的最后 4 行。我还包括了从字符串到字典的转换,以备不时之需:
*** Settings ***
Library Collections
Resource robot/resources/environment_resources.robot
*** Variables ***
${body_temp} {"message": {"Code": "26181", "rollnos": [{"ID": "1439173", "date_input": "2022-01-31 14:09:30.206748"}],"start_date": "2022-01-31 00:00:00"}}
*** Keywords ***
Converting a JSON File
${body} evaluate json.loads($body_temp) json
[Return] ${body}
*** Test Cases ***
Example
# Get body as dict
${body}= converting a json file
# Get the ID
${message} = Get From Dictionary ${body} message
${rollnos} = Get From Dictionary ${message} rollnos
${id} = Get From Dictionary ${rollnos}[0] ID
# Log the ID
Log To Console ID:${id}
Can someone please help where I went wrong in this.
您将 ${temp['rollnos']}
视为字典,但它是一个字典列表。
而不是这个:
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
执行此操作,以获取字典中第一项的 ID:
${value_1}= Set Variable ${temp['rollnos'][0]}
# ^^^
${register_id} = Set Variable ${value_1["ID"]}