Convert utf-8 codes to Unicode

Asked

Viewed 1,580 times

3

Well, I have a file JSON where all the symbols Unicode as this " " are in this format: "u2605" has some way to convert these codes to the symbols when my program Nodejs read the JSON?

Example of how it is: {"name":"\u2605 Bayonet","price":15713,"have":6,"max":6}

Example of how I want it to stay: {"name":"★ Bayonet","price":15713,"have":6,"max":6}

I even manually made one replace for these codes, but when I run it repeats twice the JSON giving replace only the first time and does not store in the variable the JSON altered.

My code:

Trade.prototype.getSteamapis = function getSteamapis(callback) {
  fs.readFile(`./prices/730.json`,'utf8',function (err,body) {
    if (err) {
      return console.log(err);
    }
    body.replace("/\u2605/g","★")
    body.replace("/\u2605 /g","★")
    body.replace("/\u9f8d/g","龍")
    body.replace("/\u58f1/g","壱")
    body.replace("/\u2122/g","™")
    body.replace("/\u5f10/g","弐")
    body.replace("/\u738b/g","王")
    console.log(body)
    return body
    });
  })
}
  • Not that it’ll solve for your case, but when you do the replace must assign to the variable: body = body.replace("/\u2605/g","★");. And one more thing: Make sure that the files are no longer being saved with the code and the Node.js is reading correctly?

  • well yes I found an error at the time of reading, but he only showed me that the raplace is not working :[

2 answers

3

First, these characters are \uXXXXX are not UTF-8, are "escaped" characters (actually any character can be escaped like this).

Second, this what you want is not to convert UTF-8 to Unicode, it is just to do the inverse of "escape", it would be an "unescape".

To understand the "supposed differences" between UTF-8 and Unicode I recommend reading this:

So is not necessary convert nothing, this is a type of "escape" to avoid "truncate the data", just do the test, at the time you popular the JSON in an HTML element it will be played as long as your page uses the charset=UTF-8.

So suppose your page has the header in the server response:

 Content-Type: text/html; charset=UTF-8

And/Or the tag (if you are a browser with HTML5 support):

 <meta charset="utf-8">

A little older browsers:

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Just popular Json (assuming the answer came from anywhere, for example via Ajax or was already inside the tag <script>):

var meujson = {"name":"\u2605 Bayonet","price":15713,"have":6,"max":6};

document.getElementById("foo-name").textContent = meujson.name;
<div id="foo-name"></div>

These escapes are needed, as I said earlier, to avoid character losses in different encodings.

0

var s = "{\"name\":\"\\u2605 Bayonet\",\"price\":15713,\"have\":6,\"max\":6}";
var s2 = JSON.parse('"' + s.replace(/\"/g, '\\"') + '"');
document.write(s2);

I based myself in this answer here at Soen and also in this one.

The idea is that the JSON.parse is able to make the necessary conversions. But how do you want to convert a string (regardless of the fact that it coincidentally is a JSON) and have a string output, the '"' + s.replace(/\"/g, '\\"') + '"' adds double quotes at the beginning and does the Escaping of double quotes that occur in the text, ensuring that the result will be a string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.