2

I'm using ASP.NET Core and I'm trying to get Twitter's user profile. The server returned a response text like this:

"{\"id\":3278814606,\"id_str\":\"3278814606\",\"name\":\"T\u00e2n Nguy\u00ea\u0303n\",\"screen_name\":\"_ntnguyen\",\"location\":\"\",\"description\":\"\",\"url\":null,\"entities\":{\"description\":{\"urls\":[]}},\"protected\":false,\"followers_count\":1,\"friends_count\":39,\"listed_count\":0,\"created_at\":\"Mon Jul 13 20:32:53 +0000 2015\",\"favourites_count\":0,\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":false,\"verified\":false,\"statuses_count\":0,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"is_translation_enabled\":false,\"profile_background_color\":\"C0DEED\",\"profile_background_image_url\":\"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_image_url_https\":\"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_tile\":false,\"profile_image_url\":\"http:\/\/pbs.twimg.com\/profile_images\/1058043873358827521\/YOOYwQY5_normal.jpg\",\"profile_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_images\/1058043873358827521\/YOOYwQY5_normal.jpg\",\"profile_banner_url\":\"https:\/\/pbs.twimg.com\/profile_banners\/3278814606\/1438769550\",\"profile_link_color\":\"1DA1F2\",\"profile_sidebar_border_color\":\"C0DEED\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"has_extended_profile\":false,\"default_profile\":true,\"default_profile_image\":false,\"following\":false,\"follow_request_sent\":false,\"notifications\":false,\"translator_type\":\"none\",\"suspended\":false,\"needs_phone_verification\":false}"

The user name is in the property:

{
    name: "T\\u00e2n Nguy\\u00ea\\u0303n"
}

How can I decode the string to the unicode character (Tân Nguyễn)?

I've tried:

console.log(decodeURIComponent("T\\u00e2n Nguy\\u00ea\\u0303n"));

but it loged with the same string.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Tân
  • 1
  • 15
  • 56
  • 102

2 Answers2

2

The problem is you're escaping the Unicode formatting with the \\ at the start. Changing the values to, for example, \u00e2n, would output the correct â:

console.log(decodeURIComponent("T\u00e2n Nguy\u00ea\u0303n"));

You could also use JSON.parse(), as per this answer.

Both of these methods will output the following:

Tân Nguyễn

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • @TânNguyễn No problem whatsoever! If my answer fixed your problem, would you mind marking my answer as accepted by clicking the greyed out tick mark to the left of my answer? – Jack Bashford Nov 03 '18 at 07:10
  • `decodeURIComponent` has nothing to do here, nor JSON.parse, `console.log("T\u00e2n Nguy\u00ea\u0303n")` will output `"Tân Nguyễn"` – Kaiido Mar 08 '19 at 01:31
0

var stringifyData = "{\"id\":3278814606,\"id_str\":\"3278814606\",\"name\":\"T\u00e2n Nguy\u00ea\u0303n\",\"screen_name\":\"_ntnguyen\",\"location\":\"\",\"description\":\"\",\"url\":null,\"entities\":{\"description\":{\"urls\":[]}},\"protected\":false,\"followers_count\":1,\"friends_count\":39,\"listed_count\":0,\"created_at\":\"Mon Jul 13 20:32:53 +0000 2015\",\"favourites_count\":0,\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":false,\"verified\":false,\"statuses_count\":0,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"is_translation_enabled\":false,\"profile_background_color\":\"C0DEED\",\"profile_background_image_url\":\"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_image_url_https\":\"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_tile\":false,\"profile_image_url\":\"http:\/\/pbs.twimg.com\/profile_images\/1058043873358827521\/YOOYwQY5_normal.jpg\",\"profile_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_images\/1058043873358827521\/YOOYwQY5_normal.jpg\",\"profile_banner_url\":\"https:\/\/pbs.twimg.com\/profile_banners\/3278814606\/1438769550\",\"profile_link_color\":\"1DA1F2\",\"profile_sidebar_border_color\":\"C0DEED\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"has_extended_profile\":false,\"default_profile\":true,\"default_profile_image\":false,\"following\":false,\"follow_request_sent\":false,\"notifications\":false,\"translator_type\":\"none\",\"suspended\":false,\"needs_phone_verification\":false}";

var parseData = JSON.parse(decodeURIComponent(stringifyData));

console.log(parseData.name);
山茶树和葡萄树
  • 2,050
  • 1
  • 18
  • 18