我试图让用户注册我的服务,每当有人用他们的名字注册拉丁美洲字符时,我都会注意到错误。我尝试阅读以下几个 SO 帖子/网站:
http://www.w3.org/TR/2009/WD-html5-20090423/infrastructure.html#character-encodings-0
http://docs.python.org/2/library/json.html
https://pypi.python.org/pypi/anyjson
但仍然无法解决。我的代码示例如下:
>>> val = json.dumps({"name":"Déjà"}, encoding="ISO-8859-1")
>>> val
'{"name": "D\\u00c3\\u00a9j\\u00c3\\u00a0"}'
无论如何强制编码在这种情况下工作和反序列化?任何帮助表示赞赏!
编辑
客户端是 Android 和 iPhone 应用程序。我正在使用以下库对客户端上的 json 进行编码:
http://loopj.com/android-async-http/ (安卓)
https://github.com/AFNetworking/AFNetworking (ios)
编辑 2 服务器从 Android 客户端收到相同的文本,如下所示:
{"NAME":"D\ufffdj\ufffd"}
我anyjson
用来反序列化它,它说:
File "/usr/local/lib/python2.7/dist-packages/anyjson/__init__.py", line 135, in loads
return implementation.loads(value)
File "/usr/local/lib/python2.7/dist-packages/anyjson/__init__.py", line 99, in loads
return self._decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 454, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 374, in decode
obj, end = self.raw_decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 393, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
ValueError: ('utf8', "D\xe9j\xe0", 1, 2, 'invalid continuation byte')