在Robot Framework中编辑JSON文件 [英] edit a JSON file in Robot Framework

查看:562
本文介绍了在Robot Framework中编辑JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将driver=1添加到我的sample_json.json文件中,但是无法找到一种方法来永久写入这些更改.此代码将键值保存到对象${json_obj},但不对文件sample_json.json

I want to add driver=1 to my sample_json.json file but can't figure out a way to write these changes permanently. This code saves the key-value to the object ${json_obj} but does not make any changes to the file sample_json.json

sample_json.json

{
    "Phones": {

        "debug": "on",

        "phone1": {
            "key":"value"            
        },

        "phone2": {
            "key":"value"
        }
    }
}

机器人文件

*** Settings ***
Library    JSONLibrary

*** Variables ***
${SUBSCRIBER_A}    phone1

*** Test Cases ***
testcase 
   test    ${SUBSCRIBER_A}

*** Keywords ***
test
    [Arguments]    ${SUBSCRIBER_A}
    ${json_obj}=    Load JSON From File     sample_json.json
    ${object_to_add}=   Create Dictionary   driver=1
    ${json_obj}=    Add Object To Json    ${json_obj}    $..${SUBSCRIBER_A}    ${object_to_add}

推荐答案

您似乎还没有完全了解该库的工作原理.当您调用Load JSON From File时,库将读取文件,并将数据作为内存中的变量(在您的情况下为${json_obj}变量)返回.

Looks like you haven't fully understood how the library works. When you call Load JSON From File, the library will read the file, and return the data as a variable in memory (in your case, your ${json_obj} variable).

现在,每当更改此数据时,您仅更改的是内存中的数据,而不是文件本身.

Now at this point, whenever you alter this data, you're only altering the data in memory, not the file itself.

您需要用新数据覆盖文件,您可以使用创建文件关键字.您可能需要先使用将JSON转换为字符串关键字.

You'll need to overwrite the file with your new data, which you can achieve using the Create File keyword. You'll likely need to convert your JSON dictionary to a string first, using the Convert JSON To String keyword.

这篇关于在Robot Framework中编辑JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆