简体   繁体   中英

Escape backslash in JSON encoding python

I m trying to JSON encode the following dict. But in this case, the message which is actually a unicode character DEVANAGARI LETTER .

So, while encoding this dict into a json object, it seems to escaping the backslash("\\") with two backslashes("\\") in message .

How do I change this to just one backslash "\\" after encoding it with json.dumps()

I m using the following custom encoder , to encode the dict to json.

class MyCustomJsonEncoder(json.JSONEncoder):
    def encode(self, obj):
        # the json obj
        count = 0
        for ob in obj:
            obj[count]['message'] = unicode(obj[count]['message']).replace("\\u","\u")
            count += 1
        return super(MyCustomJsonEncoder, self).encode(obj)

[{
    'virality': '4.6%',
    'post_engaged': 150,
    'description': '',
    'post_impressions': 1631,
    'post_story': 75,
    'name': '',
    'source': '',
    'comment_count': 16,
    'link': '',
    'text': '',
    'created_time': '03:10 AM,<br>May 13, 2013',
    'message': '\u092e\u0941\u0930\u0932\u0940 \u0938\u093e\u0930:-     \u0939\u0947 \u092e\u0940\u0920\u0947',
    'id': u'182929845081087_572281819479219',
    'status_type': 'status',
    'likes_count': 55
}]

使用unicode文字,以便理解\\u\u003c/code>转义序列,而不是让编译器认为您的意思是\\\\u\u003c/code> 。

u'\u092e....

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