php 问题 json_encode utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6058450/
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-25 23:14:24  来源:igfitidea点击:

Problem json_encode utf-8

phputf-8character-encodingjson

提问by epi82

I have a problem with json_encode function with special characters.

我对带有特殊字符的 json_encode 函数有问题。

For example I try this:

例如我试试这个:

$string="Svr?ek";

echo "ENCODING=".mb_detect_encoding($string); //ENCODING=UTF-8

echo "JSON=".json_encode($string); //JSON="Svr\u010dek"

What can I do to display the string correctly, so JSON="Svr?ek"?

我该怎么做才能正确显示字符串,所以 JSON="Svr?ek"?

Thank you very much.

非常感谢。

回答by TRiG

json_encode()is not actually outputting JSON* there. It's outputting a javascript string. (It outputs JSON when you give it an object or an array to encode.) That's fine, as a javascript string is what you want.

json_encode()实际上并没有在那里输出JSON*。它正在输出一个 javascript 字符串。(当你给它一个对象或一个数组来编码时,它会输出 JSON。)没关系,因为你想要一个 javascript 字符串。

In javascript (and in JSON), ?may be escaped as \u010d. The two are equivalent. So there's nothing wrong with what json_encode()is doing. It should work fine. I'd be very surprised if this is actually causing you any form of problem. However, if the transfer is safely in a Unicode encoding (UTF-8, usually)?, there's no need for it either. If you want to turn off the escaping, you can do so thus: json_encode('Svr?ek', JSON_UNESCAPED_UNICODE). Note that the flag JSON_UNESCAPED_UNICODEwas introduced in PHP 5.4.0, and is unavailable in earlier versions.

在 javascript(和 JSON)中,?可以转义为\u010d. 两者是等价的。因此,json_encode()正在做的事情没有错。它应该可以正常工作。如果这实际上给您带来了任何形式的问题,我会感到非常惊讶。但是,如果传输是安全的 Unicode 编码(通常是 UTF-8)?,也不需要它。如果你想关掉逃逸,你可以这样做。因此:json_encode('Svr?ek', JSON_UNESCAPED_UNICODE)。请注意,该标志JSON_UNESCAPED_UNICODE是在 PHP 5.4.0 中引入的,在早期版本中不可用。

By the way, contrary to what @onteria_ says, JSON doesuse UTF-8:

顺便说一句,与@onteria_ 所说的相反,JSON确实使用 UTF-8

The character encoding of JSON text is always Unicode. UTF-8 is the only encoding that makes sense on the wire, but UTF-16 and UTF-32 are also permitted.

JSON 文本的字符编码始终是 Unicode。UTF-8 是唯一在网络上有意义的编码,但也允许使用 UTF-16 和 UTF-32。



* Or, at least, it's not outputting JSON as defined in RFC 4627. However, there are other definitions of JSON, by which scalar values are allowed.

* 或者,至少,它不输出RFC 4627 中定义的 JSON 。不过,也有JSON的其他定义,由标值是允许的

? JSON may be in UTF-8, UTF-16LE, UTF-16BE, UFT-32LE, or UTF-32BE.

? JSON 可以是 UTF-8、UTF-16LE、UTF-16BE、UFT-32LE 或 UTF-32BE。

回答by Vulovic Vukasin

Ok, so, after you make database connection in your php script, put this line, and it should work, at least it solved my problem:

好的,所以,在您的 php 脚本中建立数据库连接后,放置这一行,它应该可以工作,至少它解决了我的问题:

mysql_query('SET CHARACTER SET utf8');

回答by onteria_

Yes, json_encodeescapes non-ascii characters. If you decode it you'll get your original result:

是的,json_encode转义非 ascii 字符。如果你解码它,你会得到你的原始结果:

$string="こんにちは";
echo "ENCODING: " . mb_detect_encoding($string) . "\n";
$encoded = json_encode($string);
echo "ENCODED JSON: $encoded\n";
$decoded = json_decode($encoded);
echo "DECODED JSON: $decoded\n";

Output:

输出:

ENCODING: UTF-8
ENCODED JSON: "\u3053\u3093\u306b\u3061\u306f"
DECODED JSON: こんにちは

EDIT: It's worth nothing that:

编辑:它毫无价值:

JSON uses Unicode exclusively.

The self-documenting format that describes structure and field names as well as specific values;

JSON 仅使用 Unicode。

描述结构和字段名称以及特定值的自记录格式;

Source: http://www.json.org/fatfree.html

来源:http: //www.json.org/fatfree.html

It uses Unicode NOTUTF-8. This FAQ Explains the difference between UTF-8 and Unicode:

它使用 Unicode 而不是UTF-8。此常见问题解答解释了 UTF-8 和 Unicode 之间的区别:

http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8

http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8

You use JSON, your non-ascii characters get escaped into Unicode code points. For example こ = code point 3053.

您使用 JSON,您的非 ascii 字符会转义为 Unicode 代码点。例如こ = 代码点 3053