Robot Framework-TypeError:解析Json时,字符串索引必须为整数 [英] Robot Framework - TypeError: string indices must be integers When Parsing Json

查看:398
本文介绍了Robot Framework-TypeError:解析Json时,字符串索引必须为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to parse an array of json, But I am getting the following error

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值,并将这些值一一存储在列表中.

I am trying to the get all the locationId values and store the values one by one inside a list.

这是我在做什么

    @{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}'

但是我遇到了同样的错误.

But I am getting the same error.

此行${locationId}= set variable ${item['locationId']}

任何帮助都会得到认可.

Any help would be apprectiated.

推荐答案

如果var ${Location_Json}的内容确实是您上面提到的json示例,则将解释异常字符串索引必须为整数".

If the content of the var ${Location_Json} is indeed the json sample you've put above, that would explain the exception "string indices must be integers".

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

The json you are working with is a dictionary (not a list); thus, in this loop:

:FOR  ${item}  IN   @{Location_Json}

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

, the value of ${item} is going to be the keys in the in the dictionary - e.g. locationId, name, etc of the top-level dictionary.

如果您对"children"子对象的"locationId"感兴趣,可以这样做-在"children"项上进行迭代:

If you are interested in the "locationId" of the "children" subdict, that will do it - iteration over the "children" items:

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

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

在每次迭代中,${item}将成为子项"中的子变量之一,您可以获取其"locationId"

On each iteration the ${item} is going to be one of the sub-dicts in "children", and you can get its "locationId"

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


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


Not related to your issue, put please do not do that:

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

请勿将${locationId}的值放在单引号内-发生的情况是,这些引号现在将成为列表成员的一部分.
假设${locationId}的值为

Do not put the value of ${locationId} inside the single quotes - what will happen is those quotes will now be a part of the list member.
Say, the value of ${locationId} is

location5

周围带有引号,它将最终显示为

with those quotes surrounding it, it will end up as

'location5'

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

I don't think that's your goal; with these extra quotes, this will fail:

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

这篇关于Robot Framework-TypeError:解析Json时,字符串索引必须为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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