您的位置:首页 > 编程语言 > Python开发

python-post-json 中文问题---微信公众号中遇到的问题

2014-03-04 14:02 561 查看
转载自http://my.oschina.net/yangyanxing/blog/159211

import
 
urllib2
02
import
 
json
03
 
04
html 
=
 
urllib2.urlopen(r
'http://api.douban.com/v2/book/isbn/9787218087351'
)
05
 
06
hjson 
=
 
json.loads(heml.read())
07
 
08
print
 
hjson[
'rating'
]
09
print
 
hjson[
'images'
][
'large'
]
10
print
 
hjson[
'summary'
]


python中json格式数据的编码和解码

http://www.01happy.com/python-json-encode-and-decode/


python编程_python通过get方式,post方式发送http请求和接收http响应_import urllib模块,import urllib2模块,import httplib模块  

http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201231085444250/

转载自http://stackoverflow.com/questions/9746303/how-do-i-send-a-post-request-as-a-json

通过python 发送post请求,并附带json信息


requrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s" %access_token
#之前的错误是因为在替换%s的时候,后面使用了逗号,以至于requrl从字符串变成了数组
#所以提示AttributeError: 'tuple' object has no attribute 'strip'

req = urllib2.Request(requrl)
req.add_header('Content-Type', 'application/json')
#重新加载系统默认的编码方式
reload(sys)
sys.setdefaultencoding('utf-8')
response = urllib2.urlopen(req, json.dumps(body,ensure_ascii=False))
#json.dumps会自动把中文转换为unicode,然后提交后,会回复{"errcode":40033,"errmsg":"invalid charset. please check your reques#t, if include \\uxxxx will create fail!"},所以只需要添加ensure_ascii=False,就可以成功添加

res = response.read()
print res



Python下调用json.dumps中文显示问题解决办法

json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如:

>>> import json
>>> js = json.loads('{"haha": "哈哈"}')
>>> print json.dumps(js)
{"haha": "\u54c8\u54c8"}

解决办法很简单:

>>> print json.dumps(js, ensure_ascii=False)
{"haha": "哈哈"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: