Json 在机器人中处理
Json handling in ROBOT
我有一个 Json 文件,其中有一个字段我需要编辑并保存文件以供下次使用。
但是我需要编辑的字段如下所示,
我需要分配给字段的值是在 运行 时间内随机生成的,我将在变量中捕获它并将其传递给这个 json 特定键 "dp"然后保存 json.
保存的 json 将用于 REST POST url。
{
"p": "10",
"v": 100,
"vt": [
{
"dp": "Field to be edited"(integer value) ,
]
}
最简单的解决方案是编写一个可以为您更改值的 python 关键字。但是,您可以通过执行以下步骤使用机器人关键字来解决此问题:
- 将 JSON 字符串转换为字典
- 修改字典
- 将字典转换回 JSON 字符串
将 JSON 字符串转换为字典
Python 有一个模块(json) for working with JSON data. You can use the evaluate keyword to convert your JSON string to a python dictionary using the loads 该模块的(加载字符串)方法。
假设您的 JSON 数据位于名为 ${json_string}
的机器人变量中,您可以将其转换为 python 字典,如下所示:
${json}= evaluate json.loads('''${json_string}''') json
有了上面的内容,${json}
现在拥有对包含所有 json 数据的字典的引用。
修改字典
Collections library that comes with robot has a keyword named set to dictionary which can be used to set the value of a dictionary element. In this case, you need to change the value of a dictionary nested inside the vt
element of the JSON object. We can reference that nested dictionary using robot's extended variable syntax.
例如:
set to dictionary ${json["vt"]} dp=the new value
有了这个,${json}
现在有了新的价值。但是,它仍然是 python 字典而不是 JSON 数据,所以还有一个步骤。
将字典转换回 JSON
将字典转换回JSON是第一步的逆过程。即,使用 json 模块的 dumps (转储字符串)方法:
${json_string}= evaluate json.dumps(${json}) json
这样,${json_string}
将包含一个有效的 JSON 字符串和修改后的数据。
完整示例
以下是一个完整的工作示例。 JSON 字符串将在替换新值之前和之后打印:
*** Settings ***
Library Collections
*** Test Cases ***
Example
${json_string}= catenate
... {
... "p": "10",
... "v": 100,
... "vt": {
... "dp": "Field to be edited"
... }
... }
log to console \nOriginal JSON:\n${json_string}
${json}= evaluate json.loads('''${json_string}''') json
set to dictionary ${json["vt"]} dp=the new value
${json_string}= evaluate json.dumps(${json}) json
log to console \nNew JSON string:\n${json_string}
为了从文件读取数据和从文件写入数据,我正在使用 OperatingSystem 库
${json} Get Binary File ${json_path}nameOfJsonFile.json
它对我有用 API 测试,阅读 .json 和 POST,就像这里
*** Settings ***
Library Collections
Library ExtendedRequestsLibrary
Library OperatingSystem
*** Variables ***
${uri} https://blabla.com/service/
${json_path} C:/home/user/project/src/json/
*** Test Cases ***
Robot Test Case
Create Session alias ${uri}
&{headers} Create Dictionary Content-Type=application/json; charset=utf-8
${json} Get Binary File ${json_path}nameOfJsonFile.json
${resp} Post Request alias data=${json} headers=${headers}
Should Be Equal As Strings ${resp.status_code} 200
对于 JSON 中的整数值,其他答案对我不起作用。
这有效:
${json}= Catenate { "p": "10", "v": 100, "vt": { "dp": "Field to be edited" } }
${value} Set Variable 2 #the value you want
${value} Convert To Integer ${value}
${json}= Evaluate json.loads('''${json}''') json
Set To Dictionary ${json["vt"]} dp=${value}
${json}= Evaluate json.dumps(${json}) json
Log ${json}
需要转换为整数,否则该值仍在字符串“${value}”中
我有一个 Json 文件,其中有一个字段我需要编辑并保存文件以供下次使用。
但是我需要编辑的字段如下所示,
我需要分配给字段的值是在 运行 时间内随机生成的,我将在变量中捕获它并将其传递给这个 json 特定键 "dp"然后保存 json.
保存的 json 将用于 REST POST url。
{
"p": "10",
"v": 100,
"vt": [
{
"dp": "Field to be edited"(integer value) ,
]
}
最简单的解决方案是编写一个可以为您更改值的 python 关键字。但是,您可以通过执行以下步骤使用机器人关键字来解决此问题:
- 将 JSON 字符串转换为字典
- 修改字典
- 将字典转换回 JSON 字符串
将 JSON 字符串转换为字典
Python 有一个模块(json) for working with JSON data. You can use the evaluate keyword to convert your JSON string to a python dictionary using the loads 该模块的(加载字符串)方法。
假设您的 JSON 数据位于名为 ${json_string}
的机器人变量中,您可以将其转换为 python 字典,如下所示:
${json}= evaluate json.loads('''${json_string}''') json
有了上面的内容,${json}
现在拥有对包含所有 json 数据的字典的引用。
修改字典
Collections library that comes with robot has a keyword named set to dictionary which can be used to set the value of a dictionary element. In this case, you need to change the value of a dictionary nested inside the vt
element of the JSON object. We can reference that nested dictionary using robot's extended variable syntax.
例如:
set to dictionary ${json["vt"]} dp=the new value
有了这个,${json}
现在有了新的价值。但是,它仍然是 python 字典而不是 JSON 数据,所以还有一个步骤。
将字典转换回 JSON
将字典转换回JSON是第一步的逆过程。即,使用 json 模块的 dumps (转储字符串)方法:
${json_string}= evaluate json.dumps(${json}) json
这样,${json_string}
将包含一个有效的 JSON 字符串和修改后的数据。
完整示例
以下是一个完整的工作示例。 JSON 字符串将在替换新值之前和之后打印:
*** Settings ***
Library Collections
*** Test Cases ***
Example
${json_string}= catenate
... {
... "p": "10",
... "v": 100,
... "vt": {
... "dp": "Field to be edited"
... }
... }
log to console \nOriginal JSON:\n${json_string}
${json}= evaluate json.loads('''${json_string}''') json
set to dictionary ${json["vt"]} dp=the new value
${json_string}= evaluate json.dumps(${json}) json
log to console \nNew JSON string:\n${json_string}
为了从文件读取数据和从文件写入数据,我正在使用 OperatingSystem 库
${json} Get Binary File ${json_path}nameOfJsonFile.json
它对我有用 API 测试,阅读 .json 和 POST,就像这里
*** Settings ***
Library Collections
Library ExtendedRequestsLibrary
Library OperatingSystem
*** Variables ***
${uri} https://blabla.com/service/
${json_path} C:/home/user/project/src/json/
*** Test Cases ***
Robot Test Case
Create Session alias ${uri}
&{headers} Create Dictionary Content-Type=application/json; charset=utf-8
${json} Get Binary File ${json_path}nameOfJsonFile.json
${resp} Post Request alias data=${json} headers=${headers}
Should Be Equal As Strings ${resp.status_code} 200
对于 JSON 中的整数值,其他答案对我不起作用。 这有效:
${json}= Catenate { "p": "10", "v": 100, "vt": { "dp": "Field to be edited" } }
${value} Set Variable 2 #the value you want
${value} Convert To Integer ${value}
${json}= Evaluate json.loads('''${json}''') json
Set To Dictionary ${json["vt"]} dp=${value}
${json}= Evaluate json.dumps(${json}) json
Log ${json}
需要转换为整数,否则该值仍在字符串“${value}”中