如何显示循环遍历后出现的字典的键值和值对

时间:2017-12-03 02:54:53

标签: robotframework

代码1有效但部分硬编码以获取城市信息,我希望同时显示所有键值对。

*** Test Cases ***
Code1
    #get json file
    ${json_data}=    Get file  detail.json
    #get  dictionaries under list
    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    ${addresslist}=  get from dictionary  ${alladdress}  addresslist
    # loop over  dictionaries under list. I wanted to use loop  FOR  ${address}   in   ${addresslist.keys()} but for some reason its not working so i use this code to display key value pair
    : FOR  ${address}  in  @{addresslist}
    \   ${city} =  Get From Dictionary  ${address}   city
    \   ${key}=  set variable  ${address.keys()}
    \   ${listkey}=  get from list  ${key}  0
    # since i know list 0 is city but its kind of hardcoded
    \   log to console  ${listkey}, ${city}
    # i am trying to display key value pair using below code but it displays error @{address.keys()}' failed: Variable '${address}' not found.
Code2
    #get json file
    ${json_data}=    Get file  detail.json
    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    ${addresslist}=  get from dictionary  ${alladdress}  addresslist
    # loop over list which contents dictionary object.
    :FOR  ${address}  IN  @{addresslist}
    \  Loop over address  @{address}
Loop over items
    [Arguments]  @{address}
    :FOR  ${key}  IN  @{address.keys()}
    \  ${value}=    Get From Dictionary    ${address}    ${key}
    # here i get error @{address.keys()}' failed: Variable '${address}' not 
       found.
    \  log to console   ${key},${value}

这是Json文件

{"class": {"id": 0,"name": "David"},"alladdress": {"count": 3,"addresslist": [{"houseno": 1,"streetno": 5,"streetname": "tesla","city": "ABC","state": "AA","country": "UK","zip": 85555},{"houseno": 2,"streetno": 6,"streetname": "honda","city": "PQR","state": "BB","country": "IN", "zip": 5252}]}}

1 个答案:

答案 0 :(得分:1)

在代码中你有这个评论:

# loop over  dictionaries under list. I wanted to use loop  FOR  ${address}
# in ${addresslist.keys()} but for some reason its not working so i use
# this code to display key value pair

:FOR ${address} IN ${addresslist.keys()}无效的原因是${addresslist}是一个列表,而不是字典。列表没有钥匙。

您需要遍历${addresslist}中的每个地址,然后在该循​​环内,您可以调用关键字来打印列表中每个元素的键/值对。

这是一个完整的工作示例:

*** Settings ***
Library  OperatingSystem
Library  Collections

*** Keywords ***
Log dictionary
    [Description]  log key/value pairs from dictionary to the console
    [Arguments]  ${dict}
    log to console  \n----
    :FOR  ${key}  IN  @{dict.keys()}
    \  ${value}=  get from dictionary  ${dict}  ${key}
    \  log to console  ${key} => ${value}

*** Test Cases ***
Code1
    ${json_data}=    Get file  detail.json

    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    @{addresslist}=  get from dictionary  ${alladdress}  addresslist

    :FOR  ${address}  in  @{addresslist}
    \  log dictionary  ${address}