Robot Framework - TypeError: string indices must be integers When Parsing Json

梦想与她 提交于 2019-12-13 03:27:17

问题


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
    }]
}

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

Here is what I am doing

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

I have also tried this

    @{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.

The test is failing on this line ${locationId}= set variable ${item['locationId']}

Any help would be apprectiated.


回答1:


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".

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

:FOR  ${item}  IN   @{Location_Json}

, 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.

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

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

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

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


来源:https://stackoverflow.com/questions/53392257/robot-framework-typeerror-string-indices-must-be-integers-when-parsing-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!