简体   繁体   中英

converting json response from web service to dictionary or object in python

I have a small python section of code -

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)

now in this function the output I am getting is something like -

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'ubuntudrive2@gmail.com', 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'ubuntudrive2@gmail.com', 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'ubuntudrive2@gmail.com', 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'ubuntudrive2@gmail.com', 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'}

Now I have tried

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 ]

but, can't access its properties. how do I do this in python ?

item.parents is a list of dicts. You need to access item.parents[0]['isRoot'] , or iterate through the values in the parents list.

you can get value by below

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM