python json文字列と辞書オブジェクトの相互変換

4612 ワード


#!/usr/bin/python
# -*- coding:utf-8 -*-

import json
js = json.loads('{"insun": "    ","name":"    "}')
js = json.loads('{"insun": "    ","name":"    "}',encoding="GBK")

print json.dumps(js)
 
  
print json.dumps(js,ensure_ascii=False)
 
  

json       ,json.loads()     utf-8,     # -*- coding:utf-8 -*- # coding=utf-8
      ,                    ;
json.dumps()     ascii,  json       ,    ensure_ascii=False,encoding="utf-8
separators=(',',':')      json         

 
  
 
  
           ,         ,    UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-16: ordinal not in range(128)   python     ascii,         :
import sys
reload(sys)
sys.setdefaultencoding("utf-8")