开发者

Javascript processing Cyrillic input

When i get a json feed from a Cyrillic site, the data is in a \ufffd format instead of Cyrillic chars.

(example feed: http://jsonduit.com/v1/f/l/7sg?cb=getJsonP_1284131679846_0开发者_开发知识库)

So when i set the source html to the input, i get weird boxes instead of characters. I tried to unescape the input but that wont work too.

How do i revert the feed back to Cyrillic?

(btw, the source page encoding is set to UTF-8)


decodeURIComponent("stringToDecodeToCyrillic")

Example: decodeURIComponent("%D0%90%D0%BB%D0%B5%D0%BA%D1%81%D0%B5%D0%B9") === "Алексей"

Fastest way to encode cyrillic letters for url


It seems you receive UTF8 string. Use the following class to decode:

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("");
    }
};

Usage:

var newString = UTF8.decode( yourString );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜