JSON编码python中的转义反斜杠

2024-06-24 13:46:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用JSON对下面的dict进行编码,但是在本例中,message实际上是一个unicode character DEVANAGARI LETTER。在

因此,在将这个dict编码为一个json对象时,它似乎用message中的两个反斜杠(“\”)来转义反斜杠(“\”)。在

在用json.dumps()编码后,如何将其更改为一个反斜杠“\”

我使用下面的custom encoder,来encodedict 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
}]

Tags: selfjsonobjmessage编码countunicodepost
1条回答
网友
1楼 · 发布于 2024-06-24 13:46:26

使用unicode文本,这样就可以理解\u转义序列,而不是让编译器认为您的意思是\\u。在

u'\u092e....

相关问题 更多 >