2017-08-22 4 views
0

Je reçois des données de la liste de MongoDB, comme ci-dessous le format:Comment convertir des données binaires mongodb en json?

[{u'school': 'search', u'name': Binary('63701838-6e3c-4ad4-ae16-4926ac0719e0', 0), u'level': 60, u'avtName': Binary('\xe8\x8b\xa6\xe7\x9c\xa0\xe4\xbd\xa0\xe5\xbf\x83', 0), u'avatarid': Binary('WEZoDaRn1Mmb0ajl', 0), u'charid': Binary('10000', 0)}] 

Comment puis-je convertir facilement ces données au format JSON?

+1

Quel est le problème avec [ 'bson.dumps'] (http://api.mongodb.com/python/current/api/bson/json_util.html) ? Ou n'ont même pas regardé là? –

+0

oui, j'ai essayé bson.dumps, le résultat est ci-dessous: {"_ id": {"$ oid": "5846707efbdc494457173262"}, "avatars": [{"school": 1, "name": {"$ binaire ":" NzRhNTY0ODQtN2NiNS00ZmYzLWI1NmMtYWJhMjVkZGVlYmE3 "," $ type ":" 00 "}," niveau ": 60," avtName ": {" $ binaire ":" 5aSn5ryg5ri45L6g "," $ type ":" 00 "}," avatarid " : {"$ binary": "V0Vad2ZtQXVXelRXSDBFRQ ==", "$ type": "00"}, "charid": {"$ binaire": "MTAwMDA =", "$ type": "00"}}], "name": {"$ binary": "NzRhNTY0ODQtN2NiNS00ZmYzLWI1NmMtYWJhMjVkZGVlYmE3", "$ type": "00"}} – nanci

+0

Il semble que les données binaires sont encodées en base64 – nanci

Répondre

0

Utilisez json_util.dumps de PyMongo:

from bson import json_util 
json_string = json_util.dumps(doc)