如何防止JSON双重编码

时间:2014-11-15 01:34:10

标签: python json python-2.7 flask

我在烧瓶框架中的python中有这个对象:

{u'_id': ObjectId('54522c44b8f9100002223fc0'), u'streetNo': u'116', u'firstName': u'Emre', u'middleName': u'n/a', u'lastName': u'S', u'creation': datetime.datetime(2014, 10, 30, 12, 17, 8, 727000), u'email': u'dean@c.edu', u'streetName': u'Columbia'}

当我去json.dumps(object,default = json_util.default)时,它变成了以下内容:

'{"_id": {"$oid": "54522c44b8f9100002223fc0"}, "streetNo": "116", "firstName": "Emre", "middleName": "n/a", "lastName": "S", "creation": {"$date": 1414671428727}, "email": "dean@c.edu", "streetName": "Columbia"}'

哪个是双重编码的,我需要在某种程度上摆脱单引号,任何想法?

1 个答案:

答案 0 :(得分:2)

一切都很好:字符串中的双引号是JSON所期望的。

外部'引号只表示这是Python中的一个字符串。

所以你有一个恰好在其中包含"的常规字符串。例如:'This is a string with " quotes'在Python中有效,因此"A string the other way around with ' quotes"