从 UTF8 JSON 到 ISO-8859-1 的字符编码
使用getJSON
检索一些我在服务器端编码的数据...
"title":"new movie \u0091The Tree of Life\u0092 on day 6"
显示的页面是字符集ISO-8859-1
并且我我正在这样做...
$.getJSON('index.php', { q: q }, function(data){
for (var i = 0; i < data.length; i++) {
alert(data[i].title + "\n" + utf8_decode(data[i].title));
}
});
utf8_decode
函数来自此处。
问题是我仍然看到两个版本的幻方...
新电影《生命之树》第 6 天
新电影 ᔨe 《生命之树》第 6 天
这让我相信也许这个角色不属于这两种编码。但是,如果我将字符串粘贴到页面上并将字符集设置为 UTF8 或 ISO-8859-1 ,它就会起作用:-/
任何帮助都会很棒!
Using getJSON
to retrieve some data which I am utf8 encoding on the server-side end...
"title":"new movie \u0091The Tree of Life\u0092 on day 6"
The page that is is displayed on is charset ISO-8859-1
and I am doing this...
$.getJSON('index.php', { q: q }, function(data){
for (var i = 0; i < data.length; i++) {
alert(data[i].title + "\n" + utf8_decode(data[i].title));
}
});
The utf8_decode
function comes from here.
The problem is that I am still seeing the magic squares for both versions...
new movie The Tree of Life on day 6
new movie ᔨe Tree of Life⠯n day 6
This leads me to believe that perhaps the character is of neither encoding. However it works if I paste the string onto a page and set the charset to either UTF8 or ISO-8859-1 :-/
Any help would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需对 JSON 传输的数据中的任何字符进行转义或解码。它是自动完成的。它也独立于页面的编码。即使 ISO-8859-1 不包含欧元符号,您也可以使用代码轻松传输和显示欧元符号 (
\u20ac
)。您的问题是字符
\u0091
和\u0092
。它们不是有效的 Unicode 字符。它们仅供私人使用。它看起来好像您实际上拥有最初使用 Windows-1250 字符集但未正确转换为 Unicode/JSON 的数据。在 Windows-1250 中,这两个字符是印刷单引号。
There is no need to escape or decode any characters in data transmitted in JSON. It's done automatically. It is also independent of the page's encoding. You can easily transmit and display the euro sign (
\u20ac
) with your code even though ISO-8859-1 does not contain the euro sign.You problem are the characters
\u0091
and\u0092
. They aren't valid Unicode characters. They are for private use only.It rather looks as if you in fact have data that originally used the Windows-1250 character set but was not properly translated to Unicode/JSON. In Windows-1250, these two characters are typographic single quotes.
您是否尝试不使用 utf8_decode ?
如果字符串中的字符存在于 ISO-8859-1 中,则这将起作用,因为 Javascript 会解码页面编码中的
\u0091
。Did you tried without utf8_decode ?
If the characters in your string exist in ISO-8859-1, this will just work, as Javascript decodes the
\u0091
in the encoding of the page.