cyc1111 发表于 2017-5-4 11:26:02

python JSON string 的中文问题

  >>>import sys
  >>>import simplejson as json
  >>>reload(sys)
  >>>sys.setdefaultencoding('utf-8')
  >>>a=u'中文'
  >>>print a
  >>> b={}
  >>> b['a']=a
  >>> print b
  {'a': u'\xd6\xd0\xce\xc4'}
  >>> import simplejson as json
  >>> json.dumps(b)
  '{"a": "\\u00d6\\u00d0\\u00ce\\u00c4"}'
  主要是这里, 这个地方传给flex我们只能得到字符串"\\u00d6\\u00d0\\u00ce\\u00c4",显然是有问题了,可能是JSON新的Spec需要这样做吧.
  加多一个参数就可以了:
  >>> print json.dumps(b,ensure_ascii=False)
  {"a": "中文"}
页: [1]
查看完整版本: python JSON string 的中文问题