python - How to encode Chinese character as 'gbk' in json, to format a url request parameter String? -
I want to dump a diction as a Jason string in which some Chinese characters are included, and its Along with the url request parameter to be formatted. / P>
Here's my Pyro code:
import httplib import simplejson json import urllib d = {"key": "æ¸ ?? æμ ·", "num ": 1} jsonStr = json.dumps (d, encoding = 'GBK') url_encode = urllib.quote_plus (jsonStr) conn = httplib.HTTPConnection (" localhost ", port = 8885) conn.request (" GET "," / " ? Json = "+ Url_encode) res = conn.getresponse () What do I expect from the request string:
get /? Json =% 7b% 22num% 22% 3A + 1% 2C +% 22key% 22% 3A +% 22% C9% CF% BA% A3% 22% 7D -------- ---- | V "% C 9% CF% BA% A3" represent "游? Æμ" in the format of 'GBK' in the URL. But what I found is:
get ? Json =% 7B% 22num% 22% 3A + 1% 2C +% 22key% 22% 3A +% 22% 5Cu6d93% 5Cu5a43% 5Cu6363% 22% 7D --------------- --------- | V% 5Cu6d93% 5Cu5a43% 5Cu6363 Chinese characters have some 'd' format "¸¸¸¸ ?? æμ ·" I have also tried to dump json with, sure Make sure that_sc = incorrect option:
jsonStr = json.dumps (D, make sure_cci = false, encoding = 'GBK') But no luck got it.
So, how can I do this work? Thank you.
You found it with almost sure_aci = incorrect . It works: jsonStr = json.dumps (d, encoding = 'gbk', make sure_aci = false) .xode ('gbk') < P> You need to tell json.dumps () that the wire that is read will be GBK, and it should not try to ASCII-Fy them. You will then have to specify the output encoding again, because json.dumps () does not have any different options for it. This solution is similar to the second answer:
So what to do for you, though I should note that the standard for the URI seems to say that whenever possible They should be in UTF-8 For more information on this, see here:
Comments
Post a Comment