在RobotFramework 显示错误列表对象中访问Dictionary 元素列表中没有属性 [英] Access Dictionary element inside the list in RobotFramework display error list object has no attribute

查看:64
本文介绍了在RobotFramework 显示错误列表对象中访问Dictionary 元素列表中没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是程序代码,我有一个json文件code.json,它的结构是这样的:[ {key:value}, {key:value} ]

Here is program code, I have one json file code.json which structure is like this : [ {key:value}, {key:value} ]

当我将列表元素转换为字典时,它会抛出错误

When I convert list element into dictionary , it throws error

*** Settings ***
Library  JSONLibrary
Library  OperatingSystem
Library  unicodelist.py
Library  Collections

*** Test Cases ***
test json data1

    ${json_obj}=    Get file  code.json
    ${getfile}=  evaluate    json.loads('''${json_obj}''')    json
    #getfile contain json of list with dictionary
    ${obj}=  Convert To List  ${getfile}
    log to console   ${obj}
    #converted sucessfully
    log to console  " Display 1"
    #just log
    ${length}=  get length  ${obj}
    log to console  ${length}
    ${list0} =  Get From List  ${obj}  0
    log to console  ${list0}
    #list0 contain first dictionary element inside the list
    ${convert}=  Convert To Dictionary  ${list0}
    log to console  ${convert}
    # no error
    log to console  " Display 2"
    ${get_item} =  Get Dictionary Keys  ${obj}
    log to console   ${get_item}
    #error list' object has no attribute 'Keys'
    log to console  " Display 3"
    ${get_item} =  Copy Dictionary   ${obj}
    log to console   ${get_item}
    # error list' object has no attribute 'copy'

推荐答案

您似乎在进行大量不必要的数据转换,并且混淆了对象的类型.

You seem to be doing a whole lot of needless converting of data, and you're confusing the types of the objects.

如果您删除所有不必要的转换,您的代码就可以正常工作.如果您使用更具描述性的变量名称,这也会有所帮助.

Your code works if you remove all of the unnecessary conversions. It also helps if you use more descriptive variable names.

示例:

*** Settings ***
Library  OperatingSystem
Library  Collections

*** Test Cases ***
test json data1

    # ${json_data} is a string of characters
    ${json_data}=    Get file  code.json

    # ${obj_list} is a list of dictionaries
    ${obj_list}=  evaluate    json.loads('''${json_data}''')    json
    log to console  \nobj_list: ${obj_list}

    # ${obj} is the first dictionary in the list
    ${obj} =  Get From List  ${obj_list}  0
    log to console  obj: ${obj}

    # ${keys} is a list of keys
    ${keys} =  Get Dictionary Keys  ${obj}
    log to console   keys: ${keys}

    # ${new_obj} is a copy of the original obj
    ${new_obj} =  Copy Dictionary   ${obj}
    log to console   new_obj: ${new_obj}

这篇关于在RobotFramework 显示错误列表对象中访问Dictionary 元素列表中没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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