我试图从列表中获取第一个键值。但是我正在获取整个列表值。
我已经尝试过机器人框架代码
"details":[{"name": "john","id": "1","add": "usa"}]
Robottest.robot->
@{det}= Set Variable ${['details']}
${type_ABC}= Evaluate type(@{det})
log to console ${type_ABC} - ##Prints TYPE <List>
:FOR ${item} IN @{det}
log to console ${item} #Prints {u'name': john', u'id': u'1'}
${dict}= Get From List ${det} 0
log to console ${dict} - dict #Expected "name":"john" but it is printing all id and add as well
预期值:第一个值“ name”:“ john”实际值:再次从JSON打印所有详细信息
在FOR循环中,您将列表${item}
用作迭代器进行循环。${item}
永远是一本字典(您目前的清单中有一部字典)。因此,在循环内部,您需要Get From Dictionary
引用当前的迭代器项以获取名称,并最终将其用作整个JSON。在列表中使用多个条目的示例:
*** Settings ***
Library Collections
*** Test Cases ***
Trying it out
&{DICT}= Create Dictionary name=john id=1 add=usa
&{ANOTHER_DICT}= Create Dictionary name=jane id=2 add=eu
@{LIST}= Create List ${DICT} ${ANOTHER_DICT}
${type_ABC}= Evaluate type(@{LIST})
log to console ${type_ABC}
: FOR ${item} IN @{LIST}
\ ${name}= Get From Dictionary ${item} name
\ log to console Name: ${name}
\ log to console Actual: ${item}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句