Javascript处理西里尔语输入

时间:2010-09-10 15:19:15

标签: javascript json

当我从西里尔语网站获得json提要时,数据采用\ ufffd格式而不是西里尔语字符。

(示例Feed:http://jsonduit.com/v1/f/l/7sg?cb=getJsonP_1284131679846_0

所以当我将源html设置为输入时,我会得到奇怪的盒子而不是字符。 我试图忽略输入,但那也不行。

如何将反馈还原为西里尔语?

(顺便说一句,源页面编码设置为UTF-8)

2 个答案:

答案 0 :(得分:3)

好像你收到了UTF8字符串。使用以下类进行解码:

UTF8 = {
    encode: function(s){
        for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
            s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
        );
        return s.join("");
    },
    decode: function(s){
        for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
        return s.join("");
    }
};

用法:

var newString = UTF8.decode( yourString );

答案 1 :(得分:3)

decodeURIComponent( “stringToDecodeToCyrillic”)

实施例: decodeURIComponent(“%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B5%D0%B9”)===“Алексей”

Fastest way to encode cyrillic letters for url