---
title: "python JSON string 的中文问题"
author: "unixboy"
date: "2011-03-15T22:28:29Z"
tags: ["Flex", "Python", "json"]
categories: ["编程语言"]
slug: "python-json-string"
draft: false
lastmod: "2011-03-15T22:30:20Z"
id: 963644
url: "https://unixboy.iteye.com/blog/963644"
---

&gt;&gt;&gt;import sys

&gt;&gt;&gt;import simplejson as json

&gt;&gt;&gt;reload(sys)

&gt;&gt;&gt;sys.setdefaultencoding('utf-8')

 

&gt;&gt;&gt;a=u'中文'

&gt;&gt;&gt;print a

 

&gt;&gt;&gt; b={}

&gt;&gt;&gt; b\['a']=a

&gt;&gt;&gt; print b

{'a': u'\\xd6\\xd0\\xce\\xc4'}

&gt;&gt;&gt; import simplejson as json

&gt;&gt;&gt; json.dumps(b)

'{"a": "\\\\u00d6\\\\u00d0\\\\u00ce\\\\u00c4"}'

 

主要是这里， 这个地方传给flex我们只能得到字符串"\\\\u00d6\\\\u00d0\\\\u00ce\\\\u00c4",显然是有问题了，可能是JSON新的Spec需要这样做吧.

 

加多一个参数就可以了:

&gt;&gt;&gt; print json.dumps(b,ensure\_ascii=False)

{"a": "中文"}