Javascript processing Cyrillic input

后端 未结 2 1151
鱼传尺愫
鱼传尺愫 2021-01-22 08:00

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_1284131

相关标签:
2条回答
  • 2021-01-22 08:12

    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 讨论(0)
  • 2021-01-22 08:22

    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

    0 讨论(0)
提交回复
热议问题