RobotFramework中列表中的Access Dictionary元素显示错误列表对象没有属性
这是程序代码,我有一个json文件code.json,结构如下: [{key:value},{key:value}] 当我将列表元素转换为字典时,它抛出错误RobotFramework中列表中的Access Dictionary元素显示错误列表对象没有属性,robotframework,Robotframework,这是程序代码,我有一个json文件code.json,结构如下: [{key:value},{key:value}] 当我将列表元素转换为字典时,它抛出错误 *** Settings *** Library JSONLibrary Library OperatingSystem Library unicodelist.py Library Collections *** Test Cases *** test json data1 ${json_obj}= Get fi
*** 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'
您似乎正在进行大量不必要的数据转换,并且混淆了对象的类型 如果您删除了所有不必要的转换,那么您的代码就可以工作。如果使用更具描述性的变量名,也会有所帮助 例如:
*** 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}
这是json文件:[{“id”:29,“name”:“small”,“city”:“AA”},{“id”:40,“name”:“medium”,“city”:“BB”},{“id”:30,“name”:“Large”,“city”:“CC”},{“id”:35,“portfolio_name”:“Large”,“city”:“DD”}]请不要将代码或数据放在注释部分。请回答您的问题,并在此处添加其他信息。@bryan,我尝试了很多次,但都不管用,所以我将其张贴在此处