简体   繁体   中英

Unescape Unicode char in javascript

I am expecting to get a string containing non ascii chars, for eg. the string Øvo , but the string I get instead contains a different representation of that char: Ã\˜vo . How can I convert this to the string I want?

I know how this string represention is obtained but I can't undo that encoding:

let modelString = JSON.stringify(model);
return window.btoa(unescape(encodeURIComponent(modelString))); 

I have been trying a lot of functions (from Firefox console) but I'm not getting the expected result:

  • window.atob(escape(decodeURIComponent('Ã\˜vo'))) -> Uncaught DOMException: String contains an invalid character
  • escape(decodeURIComponent('Ã\˜vo')) -> "%C3%98vo"
  • JSON.parse('["Ã\˜vo"]')[0] -> "Ã\˜vo"
  • JSON.parse('["Ã\˜vo"]')[0] -> "Ã\˜vo"
  • unescape('Ã\˜vo') -> "Ã\˜vo"
  • unescape('Ã\˜vo') -> "Ã\˜vo"
  • String.fromCharCode(parseInt('98',64)) -> "\"
  • String.fromCharCode(parseInt('98')) -> "b"

Avoid using unescape as it is "removed from the Web standards"!

 var model = {"key": "Øvo"}; var modelString = JSON.stringify(model); var uriEncoded = encodeURIComponent(modelString); console.log(uriEncoded); // wrong console.log(unescape(uriEncoded)); // right console.log(decodeURIComponent(uriEncoded)); // btoa convertion var b2a = btoa(decodeURIComponent(uriEncoded)); console.log(b2a); // the reverse conversion var a2b = atob(b2a); console.log(a2b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM