2014-01-27 31 views
0

我有我穿过json.loads以下JSON数据:Python的问题与得到的数据出来json.loads的

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

我试图摆脱它所有的数据,但我m无法引用数据。 我的代码如下:

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,因为它是如何存储在数据库中的。

+0

什么是你想访问或试图做? –

+0

检索每个项目的属性 – PythonLearner

回答

2

原始数据序列化的方式有问题。每个元素的attributes字典没有被序列化为一组嵌套字典,而是作为包含内部字典的Python字符串表示的外部字典。

您应该发布做过原始序列化的代码。

+0

它来自tastypie – PythonLearner

0

正如字典中的属性作为一个字符串表示我用下面的代码,将其转换为字典结束:

for object in r['objects']: 
    attrib = [] 
    attrib = ast.literal_eval(object['attributes']) 
    print attrib['code']