Encoding or charset issue with JSON payload

#1

I have an ionic application that fetches data from a remote web server (json). The response is UTF-8 and has some French characters. The issue is that these French characters are not displayed correctly in the app.
i.e. Synthétique is shown as Synthétique.
the é in utf-8 has code C3A9 (two bytes) which matches à and © in iso-8859-1 which seems to imply that there enconding from UTF-8 to iso-8859-1.
What I am doing wrong here? or what do I need to do to get the French characters correctly displayed.

1 Like
#2

You can use these tools JSON Formatter or JSON Viewer to validate json data.

#3

Did you solve it in the end? Having similar problems getting data that has formatting and its lost.

#4

Yes, I had to rely on a hack and do manual conversion on the client side. I was passing each incoming French string through the function below:

export class Hack {


    public static fromUTF8String(intpuStr) {
        let data = [];

        for (let i = 0; i < intpuStr.length; ++i) {
            data.push(intpuStr.charCodeAt(i));
        }

        var str = '',
            i;

        for (i = 0; i < data.length; i++) {
            var value = data[i];

            if (value < 0x80) {
                str = str.concat(String.fromCharCode(value));
            } else if (value > 0xBF && value < 0xE0) {
                str = str.concat(String.fromCharCode((value & 0x1F) << 6 | data[i + 1] & 0x3F));
                i += 1;
            } else if (value > 0xDF && value < 0xF0) {
                str = str.concat(String.fromCharCode((value & 0x0F) << 12 | (data[i + 1] & 0x3F) << 6 | data[i + 2] & 0x3F));
                i += 2;
            } else {
                // surrogate pair
                var charCode = ((value & 0x07) << 18 | (data[i + 1] & 0x3F) << 12 | (data[i + 2] & 0x3F) << 6 | data[i + 3] & 0x3F) - 0x010000;

                str = str.concat(String.fromCharCode(charCode >> 10 | 0xD800, charCode & 0x03FF | 0xDC00));
                i += 3;
            }
        }

        return str;
    }
}

This is not a solution I did come-up with but found it online long time ago, can’t find the original.

1 Like
#5

Thanks! Must try that!

#6

Thanks, this hack works for me!! You saved my day^^