php json_encode、json_decode 和 UTF8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3408354/
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
Json_encode, json_decode and UTF8
提问by Jake
All,
全部,
I make a JSON request to a web server using PHP and it returns me a JSON response in a variable. The JSON response will have lots of keys and values. The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.
我使用 PHP 向 Web 服务器发出 JSON 请求,它在变量中返回一个 JSON 响应。JSON 响应将有很多键和值。我从服务器获得的 JSON 响应中包含特殊字符。因此,我使用以下语句将其转换为 UTF8,解码 JSON 并将其用作数组以显示到 UI。
$response = json_decode(utf8_encode($jsonresponse));
Now, I have to pass the same value to the server in a JSON request to do some stuff. However, when I pass
现在,我必须在 JSON 请求中将相同的值传递给服务器来做一些事情。然而,当我通过
$jsonrequest = json_encode(utf8_encode($request));
to the server, it fails.
到服务器,它失败。
The following code succeeds in reading special characters and displaying it to the UI. But fails if I have to pass the utf8_encode value to the server.
以下代码成功读取特殊字符并将其显示到 UI。但是如果我必须将 utf8_encode 值传递给服务器,则会失败。
The current whole roundtrip code is as under:
当前整个往返代码如下:
$requestdata = json_encode($request);
$jsonresponse = //Do something to get from server;
$response = json_decode(utf8_encode($jsonresponse));
How can I modify it such that I pass the exact value as to what I receieved from the json response from the server?
如何修改它,以便我传递从服务器的 json 响应中收到的确切值?
回答by Artefacto
The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.
我从服务器获得的 JSON 响应中包含特殊字符。因此,我使用以下语句将其转换为 UTF8,解码 JSON 并将其用作数组以显示到 UI。
JSON data already comes encoded in UTF-8. You shouldn't convert it to UTF-8 again; you'll corrupt the data.
JSON 数据已经以 UTF-8 编码。您不应再次将其转换为 UTF-8;你会破坏数据。
Instead of this:
取而代之的是:
$response = json_decode(utf8_encode($jsonresponse));
You should have this:
你应该有这个:
$response = json_decode($jsonresponse); //already UTF-8!
回答by gwdp
Set This into your php connection :
将此设置为您的 php 连接:
$sql = “SET NAMES ‘utf8′”;
mysql_query($sql, $link);
回答by Christian Smorra
have you tryed switching the places of the functions?
你有没有试过切换功能的位置?
$jsonrequest = utf8_encode(json_encode($request));
utf8_encode only encodes strings not arrays
utf8_encode 只编码字符串而不是数组
回答by Kwebble
You convert the initial request to UTF-8, so I assume it's something else. But when you send the data back, you do not convert it back to the original encoding.
您将初始请求转换为 UTF-8,所以我假设它是别的东西。但是当您将数据发回时,您并没有将其转换回原始编码。
Are you sure you send the data in the encoding expected by the server?
您确定以服务器预期的编码发送数据吗?
回答by aletzo
I also use both ZF and utf-8 encoded strings in AJAX calls and I think that the uft8_encode and utf8_decode functions should be obsolete.
我还在 AJAX 调用中同时使用 ZF 和 utf-8 编码的字符串,我认为 uft8_encode 和 utf8_decode 函数应该已经过时了。
Do you have a valid meta tag
你有一个有效的元标签吗
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" / >
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
and a valid doctype
和有效的文档类型
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/strict.dtd">
?
?