javascript - Character encoding from UTF8 JSON to ISO-8859-1 - Stack Overflow

Using getJSON to retrieve some data which I am utf8 encoding on the server-side end..."title"

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 es 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!

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 es 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!

Share Improve this question asked Sep 5, 2011 at 15:42 firefire 21.5k18 gold badges82 silver badges116 bronze badges 1
  • 1 Fun fact: you can run a native UTF-8 decode by doing decodeURIComponent(escape(inputString)). source – sethobrien Commented Sep 5, 2011 at 17:52
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745422616a4627033.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信