【问题标题】:Robot Framework - TypeError: string indices must be integers When Parsing JsonRobot Framework - TypeError:解析Json时字符串索引必须是整数
【发布时间】:2019-04-22 20:28:39
【问题描述】:

我正在尝试解析 json 数组,但出现以下错误

TypeError: string indices must be integers

json:

{
    'locationId': 'location1',
    'name': 'Name',
    'type': 'Ward',
    'patientId': None,
    'children': [{
        'locationId': 'location2',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': [{
            'locationId': 'location3',
            'name': 'Name',
            'type': 'HospitalGroup',
            'patientId': None,
            'children': None
        }]
    }, {
        'locationId': 'location4',
        'name': 'Name',
        'type': 'Hospital',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location5',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location6',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }, {
        'locationId': 'location27',
        'name': 'Name',
        'type': 'Bed',
        'patientId': None,
        'children': None
    }]
}

我正在尝试获取所有 locationId 值并将这些值一一存储在列表中。

这就是我正在做的事情

    @{locationIds}=  create list
    :FOR  ${item}  IN   @{Location_Json}
    \  ${locationId}=  set variable  ${item['locationId']}
    \  log  ${locationId}
    \  append to list  ${locationIds}  '${locationId}'

我也试过了

    @{locationIds}=  create list
    :FOR  ${item}  IN   @{Location_Json}
    \  ${locationId}=  set variable  ${item['children'][0]['locationId']}
    \  log  ${locationId}
    \  append to list  ${locationIds}  '${locationId}'

但我遇到了同样的错误。

测试在这条线上失败${locationId}= set variable ${item['locationId']}

任何帮助都将不胜感激。

【问题讨论】:

  • 您能否通过Log Many 检查您的内存结构是否确实符合您的预期。在我看来,您正在尝试使用字符串访问一个列表。
  • @A.Kootstra 我已将${item['locationId']} 更改为${item["locationId"]},现在我只能找到第一个位置值
  • 格式为Log Many ${Location_Json_List} 注意$
  • @A.Kootstra 确实和上面的json结构一样
  • Log Many 应用到孩子身上,然后循环遍历它们。

标签: json testing robotframework


【解决方案1】:

如果 var ${Location_Json} 的内容确实是您在上面放置的 json 示例,则可以解释“字符串索引必须是整数”异常。

您正在使用的 json 是字典(不是列表);因此,在这个循环中:

:FOR  ${item}  IN   @{Location_Json}

${item} 的值将成为字典中的键 - 例如locationIdname等顶级字典。

如果您对“children” subdict 的“locationId”感兴趣,可以这样做 - 迭代“children”项:

:FOR ${item} IN @{Location_Json['children']}

在每次迭代中,${item} 将成为“children”中的子字典之一,您可以获得它的“locationId”

\    ${locationId}=  set variable  ${item['locationId']}

与您的问题无关,请不要这样做:

append to list  ${locationIds}  '${locationId}'

不要将${locationId} 的值放在单引号内 - 将会发生的是这些引号现在将成为列表成员的一部分。
比如说${locationId}的值是

location5

加上引号,它最终会变成

'location5'

我不认为这是你的目标;使用这些额外的引号,这将失败:

Should Be Equal As Strings    location5      ${locationIds[3]}   # because locationIds[3] will have the extra quotes

【讨论】:

  • 感谢您的回答,这帮助我解决了我的问题,'${locationId}' 也很不错,谢谢
  • 我能够将所有值降低一级,但我无法获得 location3
  • 听起来您需要一个通用解决方案来“获取列表/字典中可能存在的所有子字典中键 X 的所有值”,其中 X 对您来说是“locationId”。想象一下,将location3 作为值的节点在children 键中有一些东西——我猜你也想从中得到location3.1 的值。这与你输入的问题不同这个问题。
  • 没关系,我想我会为此创建一个自定义关键字,谢谢
猜你喜欢
  • 2021-08-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2020-06-24
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多