简体   繁体   English

将JSON中的“ windows-1251”编码字符串从python发送到javascript

[英]Sending “windows-1251”-encoded string in JSON from python to javascript

What I need to do is best descriped as example. 最好以我所描述的为例。 Previously, I had the following code: 以前,我有以下代码:

content = u'<?xml version="1.0" encoding="windows-1251"?>\n' + ... #
with open(file_name, 'w') as f:
     f.write(content.encode('cp1251'))
     f.close;

Now I want to modify the architecture of my entire app and send the string which is supposed to be the file content to client via JSON and to generate the file via javascript. 现在,我想修改整个应用程序的体系结构,并通过JSON将应该是文件内容的字符串发送给客户端,并通过javascript生成文件。

So, now my code looks something like this: 因此,现在我的代码如下所示:

response_data = {}
response_data['file_content'] = content.encode('cp1251')
response_data['file_name'] = file_name    
return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False)}) # error generated

The problem is that I get UnicodeDecodeError: 'ascii' codec can't decode byte 0xd4 in position 53: ordinal not in range(128) 问题是我得到UnicodeDecodeError: 'ascii' codec can't decode byte 0xd4 in position 53: ordinal not in range(128)

I also tried the second option this way: 我也这样尝试了第二种选择:

response_data = {}
response_data['file_content'] = content
response_data['file_name'] = file_name    
return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False).encode('utf8')}) # error generated

Then, on client, I try to covert utf8 to windows-1251. 然后,在客户端上,我尝试将utf8转换为Windows-1251。

 $.post ('/my_url/', data, function(response) {
        var file_content = JSON.parse(response.content).file_content;
        file_content = UnicodeToWin1251(file_content);

...but...I get distorted symbols. ...但是...我得到了扭曲的符号。 I know I am doing something terribly wrong here and am likely to mess up things with encoding, but still it's been an entire day I couldn't solve this issue. 我知道我在这里做错了非常严重的事情,很可能会使编码混乱,但是仍然整整一天我都无法解决这个问题。 Could someone give a hint where my mistake is ? 有人可以提示我的错误在哪里吗?

Both XML and JSON contain data that is Unicode text . XML和JSON都包含Unicode文本数据。 The XML declaration merely tells your XML parser how to decode the XML serialisation of that data. XML声明仅告诉您的XML解析器如何解码该数据的XML序列化。 You wrote the serialisation by hand so to match the XML header, you had to encode to CP-1251. 您是手工编写序列化的,因此要匹配XML标头,必须将其编码为CP-1251。

The JSON standard states that all JSON should be encoded in either UTF-8, UTF-16 or UTF-32, with UTF-8 the standard; JSON标准指出,所有JSON应该以UTF-8,UTF-16或UTF-32编码,标准为UTF-8; again, this is just the encoding for the serialisation . 同样,这只是序列化的编码。

Leave your data as Unicode , then encode that data to JSON with the json library; 将数据保留为Unicode ,然后使用json库将该数据编码为JSON; the library takes care of ensuring you get UTF-8 data (in Python 2), or gives you Unicode text (Python 3) that can be encoded to UTF-8 later. 该库负责确保您获取UTF-8数据(在Python 2中),或者为您提供Unicode文本(Python 3),以后可以将其编码为UTF-8。 Your Javascript code will then decode the JSON again at which point you have Unicode text again: 然后,您的Javascript代码将再次解码JSON,此时您将再次拥有Unicode文本

response_data = {}
response_data['file_content'] = content
response_data['file_name'] = file_name    
return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False)})

There is no need whatsoever to send binary data over JSON here, you are sending text. 在这里,不需要通过JSON发送二进制数据,您正在发送文本。 If you Javascript code then generates the file, it is responsible for encoding to CP-1251, not your Python code. 如果您使用Javascript代码生成了文件, 文件负责编码为CP-1251,而不是Python代码。

If you must put binary data in a JSON payload, you'll need to encode that payload to some form of text. 如果必须将二进制数据放入JSON有效负载中,则需要将该有效负载编码为某种形式的文本。 Binary data (and CP-1251-encoded text is binary data) could be encoded in text as Base-64: 二进制数据(并且CP-1251编码的文本是二进制数据)可以在文本中编码为Base-64:

import base64

response_data = {}
response_data['file_content'] = base64.encodestring(content.encode('cp1251')).decode('ascii')
response_data['file_name'] = file_name    
return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False)})

Base64 data is encoded to a bytestring containing only ASCII data, so decode it as ASCII for the JSON library, which expects text to be Unicode text. Base64数据被编码为仅包含ASCII数据的字节串,因此对于JSON库,它希望将其解码为ASCII,期望文本为Unicode文本。

Now you are sending binary data, wrapped in a Base64 text encoding, to the Javascript client, which now has to decode the Base64 if you need the binary payload there. 现在,您将以Base64文本编码包装的二进制数据发送到Javascript客户端,如果您在那里需要二进制有效负载,则Javascript客户端现在必须解码Base64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM