gpt4 book ai didi

javascript - 如何在 jquery 中解析 utf-8 json

转载 作者:可可西里 更新时间:2023-11-01 13:27:48 25 4
gpt4 key购买 nike

我在 php 文件中编码了一个波斯名字。编码前显示正确,编码后转换为 utf-8 字符。我正在通过 Ajax 在 java Script (jQuery) 中接收它。

示例.php :

json_encode('نام خانوادگی ');

//after encode: "\u06a9\u0627\u0638\u0645\u06a9\u06cc"

Js:
JSON.parse(response)
// it is still in this form: \u06a9\u0627\u0638\u0645 \u06a9\u06cc

我如何解析这个 json 以便我可以得到它的原始形式?

最佳答案

这与UTF-8无关,与Unicode有关。区别在于:

var original = "\u06a9\u0627\u0638\u0645\u06a9\u06cc"
var encoded = "\\u06a9\\u0627\\u0638\\u0645\\u06a9\\u06cc"

您必须将表示 Unicode 符号的字符串转换回原始字符串。使用这个:

var r = encoded.replace(/\\u(....)/gi, function(m,v){
return String.fromCharCode(parseInt(v,16))
})
alert(r)

关于javascript - 如何在 jquery 中解析 utf-8 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com