如何将“ \\ u00e2”或“ \\ u00ea”之类的字符解码为unicode字符?

时间:2018-11-03 06:58:45

标签: javascript jquery html string unicode

我正在使用ASP.NET Core,并且试图获取Twitter的用户个人资料。服务器返回了如下响应文本:

  

“ {\” id \“:3278814606,\” id_str \“:\” 3278814606 \“,\”名称\“:\” T \ u00e2n Nguy \ u00ea \ u0303n \“,\” screen_name \“: \“ _ ntnguyen \”,\“位置\”:\“ \”,\“描述\”:\“ \”,\“ url \”:null,\“实体\”:{\“描述\”:{ \“ 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.co m \ / 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}“

用户名位于属性中:

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

如何将字符串解码为Unicode字符(Tân Nguyễn)?

我尝试过:

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

但它记录了相同的字符串。

2 个答案:

答案 0 :(得分:2)

问题在于您开始使用\\ 转义 Unicode格式。将值更改为例如\u00e2n,将输出正确的â

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

您也可以按照this answer使用JSON.parse()

这两种方法都将输出以下内容:

  

坦Nguyễn

答案 1 :(得分: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);