机器人中的Json处理 [英] Json handling in ROBOT
问题描述
我有一个Json文件,其中有一个我需要编辑和保存文件以供下次使用的字段.
I have a Json file , in which there is a field which i need to edit and save the file for next usage.
但是我需要编辑的字段如下所示,
But the field which i need to edit is as shown below,
我需要分配该字段的val是在运行时随机生成的,我将捕获到一个变量中并将其传递给此json特定键"dp",然后保存json.
The val i need to assing fr the field is generated Randomly in run time which i'll be capturing in a variable and pass it to this json specific key "dp" then save the json.
保存的json将用于REST POST网址.
The saved json will be used for REST POST url.
{
"p": "10",
"v": 100,
"vt": [
{
"dp": "Field to be edited"(integer value) ,
]
}
请帮助我,我正在使用ROBOT框架,我需要在运行时更新json字段.
Please help me out , I am using ROBOT framework, i need to update json field in run time.
推荐答案
最简单的解决方案是编写一个可以为您更改值的python关键字.但是,您可以通过执行以下步骤来使用机器人关键字来解决此问题:
The simplest solution would be to write a python keyword that can change the value for you. However, you can solve this with robot keywords by performing the following steps:
- 将JSON字符串转换为字典
- 修改字典
- 将字典转换回JSON字符串
将JSON字符串转换为字典
Python有一个用于处理JSON数据的模块( json ).您可以使用 evaluate 关键字将JSON字符串转换为python使用该模块的加载(加载字符串)方法的字典.
Convert the JSON string to a dictionary
Python has a module (json) for working with JSON data. You can use the evaluate keyword to convert your JSON string to a python dictionary using the loads (load string) method of that module.
假设您的JSON数据位于名为${json_string}
的机器人变量中,则可以将其转换为python字典,如下所示:
Assuming your JSON data is in a robot variable named ${json_string}
, you can convert it to a python dictionary like this:
${json}= evaluate json.loads('''${json_string}''') json
有了上述内容,${json}
现在拥有对包含所有json数据的字典的引用.
With the above, ${json}
now holds a reference to a dictionary that contains all of the json data.
机器人随附的收藏库中有一个名为设置为字典可以用来设置字典元素的值.在这种情况下,您需要更改嵌套在JSON对象的vt
元素内的字典的值.我们可以使用机器人的扩展变量语法引用该嵌套字典.
The 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数据,因此还有一个步骤.
With that, ${json}
now has the new value. However, it is still a python dictionary rather than JSON data, so there's one more step.
将字典转换回JSON是第一步的反向操作.即,使用转储(转储字符串)方法json模块:
Converting the dictionary back to JSON is the reverse of the first step. Namely, use the dumps (dump string) method of the json module:
${json_string}= evaluate json.dumps(${json}) json
这样,${json_string}
将包含带有已修改数据的有效JSON字符串.
With that, ${json_string}
will contain a valid JSON string with the modified data.
以下是一个完整的工作示例. json字符串将在替换新值之前和之后输出:
The following is a complete working example. The json string will be printed before and after the substitution of the new value:
*** 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}
这篇关于机器人中的Json处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!