如何从URL参数获取unicode字符? [英] How can I get unicode characters from a URL parameter?

查看:143
本文介绍了如何从URL参数获取unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用GET请求通过JavaScript客户端将JSON发送到我的服务器,因此我开始回应响应,以确保翻译中没有丢失。似乎没有正常的文本的问题,但一旦我包括任何类型的Unicode字符(例如ç)该字符被编码的某种方式(例如\\\ç),返回值是不同的从请求值。我主要关注的是 A)在我的Python代码中保存客户端正确发送到数据库的内容, B)我将相同的值回送给客户端(当测试时)。



也许这意味着我不能使用base64,或者不得不做一些不同的事情。我确定。我的实现只是一种尝试到达目的的手段。



当前步骤(如果需要,可以更改任何步骤):



发送到服务器:

 '{weird-chars:°ç}'

JavaScript Base64编码的版本的字符串通过GET param传递给服务器(一方面,在编码字符串的末尾有等号会导致任何问题?):

  http://www.myserver.com/?json=eyJ3ZWlyZC1jaGFycyI6ICLCsMOnIn0= 

Python str 来自 b64decode 的结果:param:

 '{weird-chars:\xc2\xb0\xc3\xa7}'

Python dict 从 json.loads 解码的param:

  {'weird-chars':u'\xb0\xe7'} 

Python str json.dumps dict (并随后输出到浏览器):

 code>'{weird-chars:\\\°\\\ç}'


 >>> hex(ord(u'°'))
'0xb0'
>>>> hex(ord(u'ç'))
'0xe7'

尝试使用之前的JSON。


I need to use a GET request to send JSON to my server via a JavaScript client, so I started echoing responses back to make sure nothing is lost in translation. There doesn't seem to be a problem with normal text, but as soon as I include a Unicode character of any sort (e.g. "ç") the character is encoded somehow (e.g. "\u00e7") and the return value is different from request value. My primary concern is that, A) In my Python code saves what the client intended on sending to the database correctly, and B) I echo the same values back to the client that were sent (when testing).

Perhaps this means I can't use base64, or have to do something different along the way. I'm ok with that. My implementation is just an attempt at a means to an end.

Current steps (any step can be changed, if needed):

Raw JSON string which I want to send to the server:

'{"weird-chars": "°ç"}'

JavaScript Base64 encoded version of the string passed to server via GET param (on a side note, will the equals sign at the end of the encoded string cause any issues?):

http://www.myserver.com/?json=eyJ3ZWlyZC1jaGFycyI6ICLCsMOnIn0=

Python str result from b64decode of param:

'{"weird-chars": "\xc2\xb0\xc3\xa7"}'

Python dict from json.loads of decoded param:

{'weird-chars': u'\xb0\xe7'}

Python str from json.dumps of that dict (and subsequent output to the browser):

'{"weird-chars": "\u00b0\u00e7"}'

解决方案

Everything looks fine to me.

>>> hex(ord(u'°'))
'0xb0'
>>> hex(ord(u'ç'))
'0xe7'

Perhaps you should decode the JSON before attempting to use it.

这篇关于如何从URL参数获取unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆