问题
My Python code queries a MongoDB and gets back an array of the following objects:
{
u'attribute': u'value',
u'_id': ObjectId('534776c66e5987041f6154bd')
}
What I want to achieve, is to return the following JSON:
{
'mycollectionkey' : [
{
'attribute':'value',
'_id': ObjectId('534776c66e5987041f6154bd')
},
...and so on.
]
}
However, when I do:
docs = mongodb.find(...query...)
docs_json = bson.json_util.dumps(docs)
return flask.jsonify(success=True,mycollectionkey=docs_json)
I get: { 'mycollectionkey' : "*giant string representation of data*" }
where said giant string, clearly is no longer JSON.
回答1:
The problem here is that you already stringified the response as JSON before passing that into another data structure (now as a string) in order to return that as JSON. So you are basically doing a double encode and the "string" gets encoded.
So just pass in the data once:
docs = mongodb.find(...query...)
return bson.json_util.dumps({ 'success': True, 'mycollectionKey': docs })
So on a small collection like this:
{ "_id" : ObjectId("5343aeb5efbdb94c3647c8df"), "field" : "BBB" }
{ "_id" : ObjectId("5343aebbefbdb94c3647c8e0"), "field" : "aaa" }
{ "_id" : ObjectId("5343aebfefbdb94c3647c8e1"), "field" : "AAA" }
Yo get a result like this:
{
"mycollectionKey": [
{"field": "BBB", "_id": {"$oid": "5343aeb5efbdb94c3647c8df"}},
{"field": "aaa", "_id": {"$oid": "5343aebbefbdb94c3647c8e0"}},
{"field": "AAA", "_id": {"$oid": "5343aebfefbdb94c3647c8e1"}}
],
"success": true
}
If you are really worried about the order of those two keys then you can use the bson "dumps" to go to a string then decode with the standard json decoder in order to get a native dict with the Mongo objects deserialized, then further put into your ordered dict.
But really your client should not care about the order of the keys and just expect those root elements.
来源:https://stackoverflow.com/questions/23024356/serializing-mongodb-find-return-into-non-anonymous-json-array-using-pymongo