Javascript 如何使用 jQuery 解码 UTF8 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6497768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:02:53  来源:igfitidea点击:

How can I decode UTF8 characters using jQuery?

javascriptjqueryutf8-decode

提问by pakito

I recently just solved the issue of outputting foreign characters using utf8_decode function in this thread: How do I convert, display and store this characters in PHP?

我最近刚刚在这个线程中解决了使用 utf8_decode 函数输出外来字符的问题:如何在 PHP 中转换、显示和存储这些字符?

It works by directly echoing the results out but now I have this json_encode function to pass to jquery for the results. Json_encode is escaping my data to something like this:

它通过直接回显结果来工作,但现在我有这个 json_encode 函数可以传递给 jquery 以获取结果。Json_encode 正在将我的数据转义为以下内容:

{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}

How do I json_decode from jquery? Thank you for any advice.

我如何从 jquery json_decode?谢谢你的任何建议。

采纳答案by Quentin

Either you are writing it directly into JS, in which case do nothing or you are using one of ajax methods in jQuery, in which case just specify the dataTypeto be "json"

要么直接将其写入 JS,在这种情况下什么都不做,要么在 jQuery 中使用 ajax 方法之一,在这种情况下,只需指定dataType"json"

回答by Matchu

jQuery offers the parseJSONmethod straight from the jQueryobject:

jQueryparseJSON直接从jQuery对象中提供方法:

var data = $.parseJSON('{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}');

For fetching data via AJAX, though, $.getJSONwill run this internally and pass the result of $.parseJSONas the request's final result.

但是,对于通过 AJAX 获取数据,$.getJSON将在内部运行它并将结果$.parseJSON作为请求的最终结果传递。

回答by Rafay

try

尝试

var obj = JSON.parse('{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}');