我有一个像这样的json文件
{"downloads":[
{
"url":"arquivo1.pdf",
"descricao":"árquivo 1"
},
{
"url":"arquivo2.pdf",
"descricao":"arquivo 2"
}
]}
Run Code Online (Sandbox Code Playgroud)
我通过Notepad ++使用UTF-8编码保存它.
然后我得到文件内容:
function getContent($name)
{
$content = file_get_contents("configs/" . $name . ".json");
$encoded = utf8_encode($content);
return json_decode($encoded);
}
Run Code Online (Sandbox Code Playgroud)
并json_decode返回null.
如果我将json文件保存为ANSI,那么它可以工作.但是我想把它保存为UTF-8.
gre*_*eut 10
我怀疑初始文件已经是UTF-8或格式错误的类型.
如果无法解码json或编码数据深于递归限制,则返回NULL.
编码错误
您可以通过执行以下操作检查输入内容是否已经有效utf-8:
$is_valid_utf8 = mb_check_encoding($content, 'utf-8'));
Run Code Online (Sandbox Code Playgroud)
如果是,请不要重新编码.
该文档提供了更多内容:http://php.net/mb-check-encoding
BOM
或者Notepad ++可能会设置一个可能会混淆的BOM json_decode.
//Remove UTF-8 BOM if present, json_decode() does not like it.
if(substr($content, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) {
$content = substr($content, 3);
}
Run Code Online (Sandbox Code Playgroud)
看到json_decode的文件.
| 归档时间: |
|
| 查看次数: |
6639 次 |
| 最近记录: |