第一种:
安装并引入HttpLibrary.HTTP,使用其中的Stringify Json关键字。(安装HttpLibrary.HTTP参见https://www.jianshu.com/p/a2b1145b480f
)。
*** Settings ***
Library HttpLibrary.HTTP
*** Test Cases ***
${dict} create dictionary key1=value1 key2=value2
${json} Stringify Json ${dict}
log ${dict}
log ${json}
结果:
INFO : ${dict} = {'key1': 'value1', 'key2': 'value2'}
INFO : ${json} = {"key1": "value1", "key2": "value2"}
第二种:
安装并引入demjson,通过encode转码实现,如果字典中包含中文再通过decode转码实现。
*** Settings ***
Library demjson
*** Test Cases ***
${dict} create dictionary key1=value1 key2=value2
${json} evaluate demjson.encode(${dict},'utf-8') demjson
log ${dict}
log ${json}
${dict1} create dictionary key1=值一 key2=值二
${json1} evaluate demjson.encode(${dict1},'utf-8').decode('utf-8') demjson
log ${dict1}
log ${json1}
结果:
INFO : ${dict} = {'key1': 'value1', 'key2': 'value2'}
INFO : ${json} = {"key1":"value1","key2":"value2"}
INFO : ${dict1} = {'key1': '值一', 'key2': '值二'}
INFO : ${json1} = {"key1":"值一","key2":"值二"}
网友评论