簡體   English   中英

使用JSON編碼python轉義反斜杠

[英]Escape backslash in JSON encoding python

我正在嘗試對以下字典進行JSON編碼。 但是在這種情況下,該message實際上是一個Unicode字符DEVANAGARI LETTER

因此,當將此dict編碼為json對象時,似乎在message使用兩個反斜杠(“ \\”)來轉義反斜杠(“ \\”)。

使用json.dumps()編碼后,如何將其更改為僅一個反斜杠“ \\”

我正在使用以下custom encoder ,將字典encode為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....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM