【发布时间】: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