Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json_encode为UTF-8字符集返回null_Php_Json - Fatal编程技术网

Php json_encode为UTF-8字符集返回null

Php json_encode为UTF-8字符集返回null,php,json,Php,Json,我有一个类似这样的json文件 {"downloads":[ { "url":"arquivo1.pdf", "descricao":"árquivo 1" }, { "url":"arquivo2.pdf", "descricao":"arquivo 2" } ]} 我通过记事本+使用UTF-8编码来保存它 然后我得到文件内容: function getContent($name) { $

我有一个类似这样的json文件

{"downloads":[
    {
        "url":"arquivo1.pdf",
        "descricao":"árquivo 1"
    },
    {
        "url":"arquivo2.pdf",
        "descricao":"arquivo 2"
    }
]}
我通过记事本+使用UTF-8编码来保存它

然后我得到文件内容:

function getContent($name)
{
    $content = file_get_contents("configs/" . $name . ".json");
    $encoded = utf8_encode($content);
    return json_decode($encoded);
}
json\u decode
返回
null


如果我将json文件保存为ANSI,那么它可以工作。但我想将其另存为UTF-8。

我怀疑初始文件已经是UTF-8格式或格式不正确的类型

如果json无法解码或编码数据深度超过递归限制,则返回NULL

编码错误

通过执行以下操作,可以检查您的输入内容是否已经是有效的utf-8:

$is_valid_utf8 = mb_check_encoding($content, 'utf-8'));
如果是,不要重新编码

该文件提供了更多内容:

BOM

或者Notepad++设置了一个BOM表,这可能会混淆
json\u 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);
}

请参阅的文档。

json\u使用无BOM的UTF-8进行解码。你有什么特别的理由使用BOM表吗


如果将json文件转换为不带BOM的UTF-8,则以后无需使用utf8_encode对内容进行编码。

由于文件已在UT8中,因此无法工作,当使用
utf8_encode()
再次对其进行编码时,PHP会假定您的字符串是ISO-8859-1字符串,因此会将其中断

如果文件已在utf-8中,则无需再次对其重新编码。记事本++可以选择不使用BOM编码。现在我正在使用它。在我使用short选项之前。这就是为什么它确实不起作用。非常感谢。我不知道这是什么BOM表。