【发布时间】:2018-04-01 05:19:14
【问题描述】:
这是脚本
def validate_record_schema(record):
device = record.get('Payload', {})
manual_added= device.get('ManualAdded', None)
location = device.get('Location', None)
if isinstance(manual_added, dict) and isinstance(location, dict):
if 'Value' in manual_added and 'Value' in location:
return False
return isinstance(manual_added, bool) and isinstance(location, str)
print([validate_record_schema(r) for r in data])
这是json数据
data = [{
"Id": "12",
"Type": "DevicePropertyChangedEvent",
"Payload": [{
"DeviceType": "producttype",
"DeviceId": 2,
"IsFast": false,
"Payload": {
"DeviceInstanceId": 2,
"IsResetNeeded": false,
"ProductType": "product",
"Product": {
"Family": "home"
},
"Device": {
"DeviceFirmwareUpdate": {
"DeviceUpdateStatus": null,
"DeviceUpdateInProgress": null,
"DeviceUpdateProgress": null,
"LastDeviceUpdateId": null
},
"ManualAdded": {
"value":false
},
"Name": {
"Value": "Jigital60asew",
"IsUnique": true
},
"State": null,
"Location": {
"value":"bangalore"
},
"Serial": null,
"Version": "2.0.1.100"
}
}
}]
}]
对于device = device.get('ManualAdded', None) 行,我收到以下错误:AttributeError: 'list' object has no attribute 'get'.
请看看并帮我解决这个问题
我在哪里做错了......
我该如何解决这个错误?
请帮我解决这个问题
【问题讨论】:
-
查看数据结构,
"Payload": [...]是一个字典列表。 -
@tdelaney 我该如何解决这个问题?请帮助我
-
你已经有了问题的答案......我只是添加了原因。它是一个列表,因此您需要处理列表中的项目。您可以按照下面的建议获取第一个元素,或者将整个内容放在 for 循环中以处理所有 dicts。
-
我是 python 新手...你能修改我的函数吗?请...我不知道该怎么做
标签: json python-3.x