Python json数据中文输出问题。

这个问题困扰了我好久好久,最后看了一眼官方文档,解决问题了。

问题描述:从web上获取的json数据,然后对应的保存到了python的类型中。再次输出这个数据时,中文总会变成\u1234这种形式。

Python版本:2.7

解决方案:使用json.dump(objfpskipkeys=Falseensure_ascii=Truecheck_circular=Trueallow_nan=Truecls=Noneindent=Noneseparators=None,encoding="utf-8"default=Nonesort_keys=False**kw)

注意ensure_ascii这个参数,官方文档说,当它为false是,就会把非ASCII字符转成\uxxxx序列的形式输出。所以只要调用的时候,改下参数就好了。

If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is astr instance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.

 

 

json.dumps(your_json,ensure_ascii=False)

这货的另外一些功能就是按照key值排序,设置排版之类的。详情查看api:

http://docs.python.org/2/library/json.html

posted @ 2014-01-14 17:32  jzlikewei  阅读(471)  评论(0编辑  收藏  举报