python: json.dumps can't handle utf-8?

后端 未结 3 660
渐次进展
渐次进展 2020-12-09 16:26

Below is the test program, including a Chinese character:

# -*- coding: utf-8 -*-
import json

j = {\"d\":\"中\", \"e\":\"a\"}
json = json.dumps(j, encoding=\         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 17:07

    Use simplejson with the mentioned options:

    # -*- coding: utf-8 -*-
    import simplejson as json
    
    j = {"d":"中", "e":"a"}
    json = json.dumps(j, ensure_ascii=False, encoding="utf-8")
    
    print json
    

    Outs:

    {"e": "a", "d": "中"}
    

提交回复
热议问题