Javascript JSON.stringify() 到 UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27253150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.stringify() to UTF-8
提问by Sebastian Barth
Javascript uses as far as I know UTF-16 fundamentally as a standard for strings. With JSON.stringify() I can create a JSON string from an object.
据我所知,Javascript 从根本上使用 UTF-16 作为字符串的标准。使用 JSON.stringify() 我可以从一个对象创建一个 JSON 字符串。
Is that JSON string UTF-16 encoded?
那个 JSON 字符串是 UTF-16 编码的吗?
Can I convert (hopefully fast) that string to UTF-8 to save bandwidth for huge files (1MB JSON)?
我可以(希望很快)将该字符串转换为 UTF-8 以节省大文件(1MB JSON)的带宽吗?
采纳答案by Lightness Races in Orbit
JavaScript engines are allowed to use either UCS-2 or UTF-16.
So, yes, JSON.stringify()will return a string in whatever encoding your implementation uses for strings. If you were to find a way to change that encoding within the context of your script, it would no longer be a valid JavaScript string.
所以,是的,JSON.stringify()将以您的实现用于字符串的任何编码返回一个字符串。如果您想找到一种方法来更改脚本上下文中的编码,它将不再是有效的 JavaScript 字符串。
For serialising it over a network, though, I would expect it to automatically be transcoded into the character set of the HTTP request (assuming you're talking about HTTP). So if you send it via HTTP POST with a character set of UTF-8, your browser should transparently handle the transcoding of that data before it is sent.
但是,为了通过网络对其进行序列化,我希望它会自动转码为 HTTP 请求的字符集(假设您在谈论 HTTP)。因此,如果您通过带有 UTF-8 字符集的 HTTP POST 发送它,您的浏览器应该在发送之前透明地处理该数据的转码。
Otherwise browsers would really struggle with character set handling.
否则浏览器真的会在字符集处理上挣扎。

