json_encode返回NULL,json_last_error_msg给出“控制字符错误,可能编码错误". [英] json_encode returns NULL, json_last_error_msg gives "Control character error, possibly incorrectly encoded"

查看:81
本文介绍了json_encode返回NULL,json_last_error_msg给出“控制字符错误,可能编码错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读入我的编辑器后,文件看起来不错.

The file looks fine when read into my editor.

$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

关于此错误消息的含义没有很多.

There isn't much out there on what this error message means.

推荐答案

,您可以删除控制字符,PCRE支持POSIX符号用于字符类 [:cntrl:]

you can remove the control character, PCRE supports the POSIX notation for character classes [:cntrl:]

$json = preg_replace('/[[:cntrl:]]/', '', $json);
$json = json_decode($json, true);
var_dump($json);
echo json_last_error_msg();

这篇关于json_encode返回NULL,json_last_error_msg给出“控制字符错误,可能编码错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆