2014-09-26 149 views
1

我有一个很小的代码蟒蛇部分 -转换JSON响应字典或对象在Python

self.drive_service = build('drive', 'v2', http=self.http)   
result = self.retrieve_all_files(self.drive_service) 
print "result recieved \n" 
rootNode = root() 
for item in result: 
    print "item \n" 
    print item 
    if(item["parents"]["isRoot"]==true): 
     node = root() 
     node.setData(item) 
     rootNode.add_child(node) 
    else: 
     node = rootNode.findParent(item["parents"]["id"]) 
     print node 
     newNode = root() 
     newNode.setData(item) 
     node.add_child(newNode) 
现在在这个函数的输出我得到

是一样的东西 -

item 

{u'mimeType': u'application/vnd.google-apps.document', u'version': u'31', u'appDataContents': False, u'thumbnailLink': u'https://docs.google.com/feeds/vt?gd=true&id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&v=0&s=AMedNnoAAAAAVCVIbiMrMSR6YCxDI4U4t3VByW0yl8Ef&sz=s220', u'labels': {u'restricted': False, u'starred': False, u'viewed': True, u'hidden': False, u'trashed': False}, u'etag': u'"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/MTQxMTcxMzg5MjE4OA"', u'lastModifyingUserName': u'Harshit Laddha', u'writersCanShare': True, u'owners': [{u'emailAddress': u'[email protected]', u'kind': u'drive#user', u'isAuthenticatedUser': True, u'displayName': u'Harshit Laddha', u'permissionId': u'07000837325456913137'}], u'id': u'1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o', u'lastModifyingUser': {u'emailAddress': u'[email protected]', u'kind': u'drive#user', u'isAuthenticatedUser': True, u'displayName': u'Harshit Laddha', u'permissionId': u'07000837325456913137'}, u'title': u'DocumentTest', u'ownerNames': [u'Harshit Laddha'], u'lastViewedByMeDate': u'2014-09-26T06:44:52.188Z', u'parents': [{u'isRoot': True, u'kind': u'drive#parentReference', u'id': u'0AKIx1lrV8B4CUk9PVA', u'selfLink': u'https://www.googleapis.com/drive/v2/files/1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o/parents/0AKIx1lrV8B4CUk9PVA', u'parentLink': u'https://www.googleapis.com/drive/v2/files/0AKIx1lrV8B4CUk9PVA'}], u'exportLinks': {u'text/html': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=html', u'application/pdf': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=pdf', u'application/vnd.openxmlformats-officedocument.wordprocessingml.document': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=docx', u'application/vnd.oasis.opendocument.text': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=odt', u'application/rtf': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=rtf', u'text/plain': u'https://docs.google.com/feeds/download/documents/export/Export?id=1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o&exportFormat=txt'}, u'shared': False, u'editable': True, u'kind': u'drive#file', u'markedViewedByMeDate': u'1970-01-01T00:00:00.000Z', u'modifiedDate': u'2014-09-26T06:44:52.188Z', u'createdDate': u'2014-09-26T06:44:47.731Z', u'iconLink': u'https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png', u'embedLink': u'https://docs.google.com/document/d/1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o/preview', u'alternateLink': u'https://docs.google.com/document/d/1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o/edit?usp=drivesdk', u'copyable': True, u'modifiedByMeDate': u'2014-09-26T06:44:52.188Z', u'userPermission': {u'kind': u'drive#permission', u'etag': u'"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/8agI6QAPPmC1EpQ7iWetUjyhaKM"', u'role': u'owner', u'type': u'user', u'id': u'me', u'selfLink': u'https://www.googleapis.com/drive/v2/files/1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o/permissions/me'}, u'quotaBytesUsed': u'0', u'selfLink': u'https://www.googleapis.com/drive/v2/files/1YkXBuzc60VJAPrpJbPloJhWv4u_9h6-t6Phll5HMu9o'} 
item 

{u'mimeType': u'application/vnd.google-apps.folder', u'version': u'17', u'appDataContents': False, u'labels': {u'restricted': False, u'starred': False, u'viewed': True, u'hidden': False, u'trashed': False}, u'etag': u'"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/MTQxMTcxMzg3MDM3NA"', u'lastModifyingUserName': u'Harshit Laddha', u'writersCanShare': True, u'owners': [{u'emailAddress': u'[email protected]', u'kind': u'drive#user', u'isAuthenticatedUser': True, u'displayName': u'Harshit Laddha', u'permissionId': u'07000837325456913137'}], u'id': u'0B6Ix1lrV8B4CZ05ELV9yWFF5QkE', u'lastModifyingUser': {u'emailAddress': u'[email protected]', u'kind': u'drive#user', u'isAuthenticatedUser': True, u'displayName': u'Harshit Laddha', u'permissionId': u'07000837325456913137'}, u'title': u'TestFolder', u'ownerNames': [u'Harshit Laddha'], u'lastViewedByMeDate': u'2014-09-26T06:45:10.151Z', u'parents': [{u'isRoot': True, u'kind': u'drive#parentReference', u'id': u'0AKIx1lrV8B4CUk9PVA', u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B6Ix1lrV8B4CZ05ELV9yWFF5QkE/parents/0AKIx1lrV8B4CUk9PVA', u'parentLink': u'https://www.googleapis.com/drive/v2/files/0AKIx1lrV8B4CUk9PVA'}], u'shared': False, u'editable': True, u'markedViewedByMeDate': u'1970-01-01T00:00:00.000Z', u'modifiedDate': u'2014-09-26T06:44:30.374Z', u'createdDate': u'2014-09-26T06:44:30.543Z', u'iconLink': u'https://ssl.gstatic.com/docs/doclist/images/icon_11_collection_list.png', u'kind': u'drive#file', u'alternateLink': u'https://docs.google.com/folderview?id=0B6Ix1lrV8B4CZ05ELV9yWFF5QkE&usp=drivesdk', u'copyable': False, u'modifiedByMeDate': u'2014-09-26T06:44:30.374Z', u'userPermission': {u'kind': u'drive#permission', u'etag': u'"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/6AaXQGE6dCZ2Vl4_byZGrb_EsJk"', u'role': u'owner', u'type': u'user', u'id': u'me', u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B6Ix1lrV8B4CZ05ELV9yWFF5QkE/permissions/me'}, u'quotaBytesUsed': u'0', u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B6Ix1lrV8B4CZ05ELV9yWFF5QkE'} 

现在我试过了

item["parents"] 
item.parents 
json.loads(result) ["which says not a string or buffer as result is an array object so tried str(result) too ] 

但是,无法访问它的属性。我如何在Python中做到这一点?

回答

0

item.parents是一个字典列表。您需要访问item.parents[0]['isRoot'],或遍历父级列表中的值。

0

您可以通过以下

json_data={u'mimeType': u'application/vnd.google-apps.folder', u'version': u'17', u'appDataContents': False, u'labels':u'aaaa'} 
data = json.dumps(json_data) 
json_to_python = json.loads(data) 
print (json_to_python['version']) 
获得价值