I'm using the Google Maps API. Please see this JSON response .
The HTML instructions is written like this:
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eEnggårdsgade\u003c/b\u003e"
How can I convert the unicodes \<
, \>
etc. in JavaScript?
Those are Unicode character escape sequences in a JavaScript string. As far as JavaScript is concerned, they are the same character.
'\u003cb\u003eleft\u003c/b\u003e' == '<b>left</b>'; // true
So, you don't need to do any conversion at all.
Below is a simpler way thanks to modern JS .
ES6 / ES2015 introduced the normalize() method on the String prototype, so we can do:
var directions = "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eEnggårdsgade\u003c/b\u003e";
directions.normalize();
//it will return : "Turn <b>left</b> onto <b>Enggårdsgade</b>"
Refer to this article : https://flaviocopes.com/javascript-unicode/
您可以直接在 JSON 响应上使用JSON.parse ,然后 unicode 字符将自动转换为其 html 计数器部分(\< 将转换为 < 登录 html)
JSON.parse(JSON.stringify({a : 'Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eEnggårdsgade\u003c/b\u003e'}));
This small function may help
String.prototype.toUnicode = function(){
var hex, i;
var result = "";
for (i=0; i<this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("\\u00"+hex).slice(-7);
}
return result;
};
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.