【发布时间】:2011-04-12 16:31:32
【问题描述】:
我正在构建一个使用 json-rpc 与 django 后端通信的应用程序。到目前为止,一切都运行良好。但是我在发送“ ”时发现异常。据我所知,请求工作正常,但是 django 对响应的解释很糟糕。我在下面复制了一个简化的请求和响应:
请求:
{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}
Django 接收:
<QueryDict:u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello ': [u''], u'nbsp': [u''], u'there"}}': [u'']}>
预期响应:
<QueryDict: {u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}': [u'']}>
似乎 django 解释了 & 和 ;作为特殊字符,因此会在其 request.POST 变量中创建一个意外的字典。
我需要做些什么来确保 json 字符串不会出现格式错误?我曾尝试使用 php htmlspecialchars() 方法对其进行编码,但由于这并没有删除“&”,因此问题仍然存在。
任何帮助将不胜感激。
【问题讨论】:
-
解决方法:如果只是 nbsp,则在此处使用原始 UTF-8 字符。
-
感谢您的建议。我已经尝试过了,但我认为 django 在我有机会之前会转换字符。到目前为止,我刚刚手动删除了空格,这对我现在有用。
-
默认字符集为 UTF-8,除非您在设置中指定其他内容。 docs.djangoproject.com/en/dev/ref/settings/#default-charset.
标签: json html-parsing django-views