1

I have chars in a response that can not be decoded.

Charakters like ß that represents ß and – that represents a - are causing the exception and these chars like ß and - is what i want at the end. What i am doing right now is replacing the chars that throws an exception before calling decodeURIComponent().

I know it's bad but i don't know how to handle that correctly.

var jsonResponse = "WG_ Ihre Buchung ist bestätigt – 1232456201 FRANKFURT AM MAIN Oktober 16_ 2020 nach Mußterstadt, Mußterstraße 11,  Oktober 17_ 2020.msg";
var finalString = jsonResponse.replace(/ß/g,"ss").replace(/–/g,"-");
console.log("escaped String: "+ escape(finalString));
var decoded = decodeURIComponent(escape(finalString));
console.log(decoded);

How can I convert this correctly?

HymnZzy
  • 2,669
  • 1
  • 17
  • 27
Rocky
  • 27
  • 7
  • Are you sure you want to use `escape` and not `encodeURIComponent`? Also, you cannot keep replacing items for long, ensure that the data is coming with proper utf-8 encoding . – HymnZzy Dec 01 '20 at 17:44
  • I am using escape and decodeURIComponent or what do you mean? I am not sure that the data is coming in UTF-8 encoding but when sending the request is set Content-Type application/json and charset= UTF-8. – Rocky Dec 01 '20 at 17:50
  • try using `encodeURIComponent` instead of `escape` on the `jsonResponse` . That should solve your issue here – HymnZzy Dec 01 '20 at 17:52
  • when calling encodeURIComponent(jsonResponse) result is WG_%20Ihre%20Buchung%20ist%20best%C3%83%C2%A4tigt%20%C3%A2%E2%82%AC%E2%80%9C%201232456201%20FRANKFURT%20AM%20MAIN%20Oktober%2016_%202020%20nach%20Mu%C3%83%C5%B8terstadt%2C%20Mu%C3%83%C5%B8terstra%C3%83%C5%B8e%2011%2C%20%20Oktober%2017_%202020.msg **but the result that i am expecting should look like WG_ Ihre Buchung ist bestätigt - 1232456201 FRANKFURT AM MAIN Oktober 16_ 2020 nach Mußterstadt, Mußterstraße 11, Oktober 17_ 2020.msg** – Rocky Dec 01 '20 at 18:01
  • Correct. `encodeURIComponent` is the reverse of `decodeURIComponent` . If you apply `decodeURIComponent` on this string above, you'll get the original string. – HymnZzy Dec 01 '20 at 18:03
  • Thats nice but not what i want. The result should look like **WG_ Ihre Buchung ist bestätigt - 1232456201 FRANKFURT AM MAIN Oktober 16_ 2020 nach Mußterstadt, Mußterstraße 11, Oktober 17_ 2020.msg** – Rocky Dec 01 '20 at 18:08
  • There are two solutions here. 1. fix the header, it is very likely treating the string as window-1252 encoding. Option 2. Use setup a conversion table using this - https://www.i18nqa.com/debug/utf8-debug.html . `escape` function will be deprecated and hence should not be used. – HymnZzy Dec 01 '20 at 18:31
  • In your example, `jsonResponse` is already broken. Is that the situation you're facing? Any chance to just get data fixed in source? – Álvaro González Dec 02 '20 at 08:53
  • @ÁlvaroGonzález Yes that's the situation that i am facing. The `jsonResponse` is exactly the data that i receive in the Response from the Server. When using `decodeURIComponent(escape("bestätigt"))` returns `bestätigt` and that is correct and exactly what i want to have but its not working for `–` which represents a - and throws the exception. – Rocky Dec 02 '20 at 09:09
  • Sorry if I wasn't clear. If you receive damaged data, fixing it afterwards will never be reliable (how can you know in advance what else the server is mangling?). Why not try to prevent corruption from happening? A simile: if your phone's SD card is damaged, you don't get an app to recover damaged pictures and call it a day; you fix or replace your SD card. – Álvaro González Dec 02 '20 at 09:48
  • o.k understand your thoughts thanks for that. It's not a Server that is in my control its a external Server in other words a Cloudsystem. But i was able to determine the following. These errors only occur with files that are uploaded to the server by using the Cloud Application. This can be for example PDF files or msg files that contains such kind of characters like **-**. If the same characters within the Cloud Application is used elsewhere, these errors do not occur. Sounds strange i know... – Rocky Dec 02 '20 at 15:34

0 Answers0