从json.loads中获取数据的Python问题
我通过JSON.loads传递了以下JSON数据:从json.loads中获取数据的Python问题,python,json,Python,Json,我通过JSON.loads传递了以下JSON数据: { "meta":{ "limit":20, "next":null, "offset":0, "previous":null, "total_count":2 }, "objects":[ { "attributes":"{u'_code': u'[ON CODE]/[OFF CODE]', u'_type': u'pick actua
{
"meta":{
"limit":20,
"next":null,
"offset":0,
"previous":null,
"total_count":2
},
"objects":[
{
"attributes":"{u'_code': u'[ON CODE]/[OFF CODE]', u'_type': u'pick actuator or sensor', u'code': u'AF126E/AF1266', u'type': u'actuator'}",
"id":1,
"module":"/api/v1/module/1/",
"moduleName":"rfm_ninjablock (ninjablock)",
"name":"HallwayLight"
},
{
"attributes":"{u'_code': u'[ON CODE]/[OFF CODE]', u'_type': u'pick actuator or sensor', u'code': u'0x53df5c', u'type': u'sensor'}",
"id":2,
"module":"/api/v1/module/1/",
"moduleName":"rfm_ninjablock (ninjablock)",
"name":"ToiletDoor"
}
]
}
我正试图从中获取所有数据,但在引用数据时遇到了问题。
我的代码如下:
for object in r['objects']:
for attributes in object.iteritems():
print attributes
这给了我:
(u'attributes', u"{u'_code': u'[ON CODE]/[OFF CODE]', u'_type': u'pick actuator or sensor', u'code': u'AF126E/AF1266', u'type': u'actuator'}")
(u'moduleName', u'rfm_ninjablock (ninjablock)')
(u'id', 1)
(u'module', u'/api/v1/module/1/')
(u'name', u'HallwayLight')
(u'attributes', u"{u'_code': u'[ON CODE]/[OFF CODE]', u'_type': u'pick actuator or sensor', u'code': u'0x53df5c', u'type': u'sensor'}")
(u'moduleName', u'rfm_ninjablock (ninjablock)')
(u'id', 2)
(u'module', u'/api/v1/module/1/')
(u'name', u'ToiletDoor')
我不确定是否引用了这些,或者我是否真的做对了
属性已经包含JSON,因为它是如何存储在数据库中的。原始数据的序列化方式有问题。每个元素的
属性
dict没有序列化为一组嵌套dict,而是作为包含内部dict的Python字符串表示的外部dict
您应该发布执行原始序列化的代码。由于dict-inside属性最终成为字符串表示形式,我使用以下代码将其转换为dict:
for object in r['objects']:
attrib = []
attrib = ast.literal_eval(object['attributes'])
print attrib['code']
您正在尝试访问或尝试执行什么操作?检索每个项目的属性