How can I convert a dict to a unicode JSON string?

后端 未结 2 901
情话喂你
情话喂你 2021-02-09 02:05

This doesn\'t appear to be possible to me using the standard library json module. When using json.dumps it will automatically escape all non-ASCII char

2条回答
  •  一向
    一向 (楼主)
    2021-02-09 02:33

    encode_ascii=False is the best solution IMHO.

    If you are using Python2.7, here is example python file :

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # example.py
    from __future__ import unicode_literals
    from json import dumps as json_dumps
    d = {'navn': 'Åge', 'stilling': 'Lærling'}
    print json_dumps(d, ensure_ascii=False).encode('utf-8')
    

提交回复
热议问题