Json 属性错误:';列表';对象没有属性';获取';?
这是剧本Json 属性错误:';列表';对象没有属性';获取';?,json,python-3.x,Json,Python 3.x,这是剧本 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(locatio
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'对象没有属性“get”。
请看一看,帮我解决这个问题
在我犯错误的地方
如何修复此错误
请帮助我解决此问题正如错误提示的那样,您无法在列表中
.get()
。要获取位置和手动添加字段,可以使用:
manual_added = record.get('Payload')[0].get('Payload').get('Device').get('ManualAdded')
location = record.get('Payload')[0].get('Payload').get('Device').get('Location')
因此,您的功能将变成:
def validate_record_schema(record):
manual_added = record.get('Payload')[0].get('Payload').get('Device').get('ManualAdded')
location = record.get('Payload')[0].get('Payload').get('Device').get('Location')
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)
请注意,这会将位置设置为
{
"value":"bangalore"
}
并增加了手动操作单元
{
"value":false
}
在遍历
数据时跟踪类型时遇到问题。一个技巧是在调试过程中添加打印,以查看发生了什么。例如,顶部的“Payload”对象是dict
的列表,而不是单个dict
。这个列表意味着可以有多个设备描述符,因此我编写了一个示例,检查所有设备描述符,如果在过程中发现错误,则返回False。您可能需要根据您的验证规则来更新它,但这将帮助您开始
def validate_record_schema(record):
"""Validate that the 0 or more Payload dicts in record
use proper types"""
err_path = "root"
try:
for device in record.get('Payload', []):
payload = device.get('Payload', None)
if payload is None:
# its okay to have device without payload?
continue
device = payload["Device"]
if not isinstance(device["ManualAdded"]["value"], bool):
return False
if not isinstance(device["Location"]["value"], str):
return False
except KeyError as e:
print("missing key")
return False
return True
你能在同一个函数中修改吗?我已经扩展了答案,包括函数中的上下文。感谢你宝贵的帮助…我被困了两天…非常感谢你查看数据结构,“有效负载”:[…]
这是一个目录列表。@tdelaney如何解决此问题?请帮助我您已经找到了问题的答案。。。。我只是补充一下原因。这是一个列表,因此您需要处理列表中的项目。你可以得到下面建议的第一个元素,或者把整个东西放在for循环中来处理所有的dict。我是python新手…你能修改我的函数吗?请…我不知道该做什么谢谢你的帮助…我被困了两天…非常感谢你