PHP json_encode json_decode UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4076988/
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
PHP json_encode json_decode UTF-8
提问by FFish
How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?
如何将带有国际字符的 json 编码字符串保存到数据库中,然后在浏览器中解析解码后的字符串?
<?php
$string = "très agréable";
// to the database
$j_encoded = json_encode(utf8_encode($string));
// get from Database
$j_decoded = json_decode($j_encoded);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<?= $j_decoded ?>
</html>
回答by Lukas Liesis
json utf8 encode and decode:
json utf8 编码和解码:
json_encode($data, JSON_UNESCAPED_UNICODE)
json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)
force utf8 might be helpfull too: http://pastebin.com/2XKqYU49
force utf8 也可能有帮助:http: //pastebin.com/2XKqYU49
回答by Ahmet Erkan ?EL?K
header('Content-Type: application/json; charset=utf-8');
回答by Pekka
This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.
这是编码问题。看起来在某些时候,数据被表示为 ISO-8859-1。
Every part of your process needs to be UTF-8 encoded.
流程的每个部分都需要进行 UTF-8 编码。
The database connection
The database tables
Your PHP file (if you are using special characters inside that file as shown in your example above)
The
content-type
headers that you output
数据库连接
数据库表
您的 PHP 文件(如果您在该文件中使用特殊字符,如上例所示)
content-type
您输出的标题
回答by Bernd Ott
If your source-file is already utf8 then drop the utf8_* functions. php5 is storing strings as array of byte.
如果您的源文件已经是 utf8,则删除 utf8_* 函数。php5 将字符串存储为字节数组。
you should add a meta tag for encoding within the html AND you should add an http header which sets the transferencoding to utf-8.
您应该在 html 中添加一个用于编码的元标记,并且您应该添加一个 http 标头,将 transferencoding 设置为 utf-8。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
and in php
并在 php 中
<?php
header('Content-Type: text/html; charset=utf-8');
回答by Coquevas
Try sending the UTF-8 Charset header:
尝试发送 UTF-8 字符集标头:
<?php header ('Content-type: text/html; charset=utf-8'); ?>
And the HTML meta:
和 HTML 元:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
回答by teemitzitrone
- utf8_decode
$j_decoded = utf8_decode(json_decode($j_encoded));
EDITor to be more correct$j_encoded = json_encode($j_encoded);
$j_decoded = json_decode($j_encoded);
no need for en/decoding utf8 <meta charset="utf-8" />
- utf8_decode
$j_decoded = utf8_decode(json_decode($j_encoded));
编辑或更正确的$j_encoded = json_encode($j_encoded);
$j_decoded = json_decode($j_encoded);
不需要编码/解码utf8 <meta charset="utf-8" />
回答by EVGENIY DANILOV
For me both methods
对我来说这两种方法
<?php
header('Content-Type: text/html; charset=utf-8');
echo json_encode($YourData, \JSON_UNESCAPED_UNICODE);
回答by Micha? Moskal
I had the same problem. It might differ depending on how You put the data to the db, but try what worked for me:
我有同样的问题。它可能会因您将数据放入数据库的方式而异,但请尝试对我有用的方法:
$str = json_encode($data);
$str = addslashes($str);
Do this before saving data to db.
在将数据保存到 db 之前执行此操作。
回答by Blox
if you get "unexpected Character" error you should check if there is a BOM (Byte Order Marker saved into your utf-8 json. You can either remove the first character or save if without BOM.
如果您收到“意外字符”错误,您应该检查是否有 BOM(字节顺序标记保存到您的 utf-8 json 中。如果没有 BOM,您可以删除第一个字符或保存。
回答by Douglas Comim
Work for me :)
为我工作:)
function jsonEncodeArray( $array ){
array_walk_recursive( $array, function(&$item) {
$item = utf8_encode( $item );
});
return json_encode( $array );
}