国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

python - 微信中網(wǎng)頁授權(quán)獲取用戶基本信息后得到中文亂碼
阿神
阿神 2017-04-17 15:38:02
0
3
1039

打印出返回的用戶信息的json值,如果是中文會(huì)出現(xiàn)亂碼,比如省份,得到的值為u'province': u'\xe6\xb5\x99\xe6\xb1\x9f', 不知道哪里出錯(cuò)了。
另外,如果對(duì)返回的json值作解碼,居然會(huì)出現(xiàn)40029錯(cuò)誤。

阿神
阿神

閉關(guān)修行中......

reply all(3)
小葫蘆

That's right. The questioner, please try printing 'xe6xb5x99xe6xb1x9f'. Doesn't this mean "Zhejiang" is printed out?

First of all, there is a bug in python2's print, that is, although x = {'province': 'Zhejiang'}, print x will still display {'province': 'xe6xb5x99xe6xb1x9f'}.

This problem has been solved in Python3.

But look carefully this is not your case. Your problem is that the utf8-encoded string is treated as a unicode-encoded string and put into the unicode object.

At this time only (assuming that the object after your json deserialization exists in x)

python>>> print x[u'province'].encode('raw_unicode_encoding')
'浙江'

Voilà!

PHPzhong

It should be that the encoding format is not set. When obtaining user information, set the encoding to UTF-8. In addition, the parameters https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN in the interface for obtaining user information lang=zh_CN should be brought.
As for the 40029 error, it may be that the code obtained through authorization has expired. The code can only be used once and will automatically expire if not used for 5 minutes.

大家講道理

@lohocla4dam's answer helped me a lot, but I need to make some additions, because his answer is for python2, and my answer here is for python3

According to https://docs.python.org/3/library/codecs..., the encoding is changed to raw_unicode_escape, which is

>>> print(x['province'].encode('raw_unicode_escape')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template