当我将文件读入我的编辑器时,它看起来很好。
$file = file_get_contents('path/to/file.json');
$json = json_decode($file, true);
var_dump($json); // null
echo json_last_error_msg(); //Control character error, possibly incorrectly encoded
关于这个错误信息的含义,目前并没有太多相关内容。
当我将文件读入我的编辑器时,它看起来很好。
$file = file_get_contents('path/to/file.json');
$json = json_decode($file, true);
var_dump($json); // null
echo json_last_error_msg(); //Control character error, possibly incorrectly encoded
关于这个错误信息的含义,目前并没有太多相关内容。
您可以删除控制字符,PCRE支持 字符类的POSIX表示法[:cntrl:]
$json = preg_replace('/[[:cntrl:]]/', '', $json);
$json = json_decode($json, true);
var_dump($json);
echo json_last_error_msg();
preg_replace
会修复它,但你最终得到的是 "helloworld"。应该修复生成 JSON 编码字符串的代码而不是用 preg_replace
。 - vbarbarosh
file.json
文件吗?因为json_decode
无法解码您从文件中读取的 JSON 字符串。 - Ravi Dhoriya ツ1024
个字符,而整行长度超过了这个数字。这就导致了问题。然后更新长度解决了这个问题。”被接受的答案没有帮助。 - ssi-anik