Javascript Can';t json_decode()来自文件-语法错误

Javascript Can';t json_decode()来自文件-语法错误,javascript,php,json,syntax,Javascript,Php,Json,Syntax,我被这个问题困住了。这是我的密码: 您调用的json\u decode参数错误。第一个参数是JSON数据,而不是文件名!因此,如果要从文件中解析JSON,可以编写 json_decode(file_get_contents('mailconfig.json'), true); 您调用的json\u decode参数错误。第一个参数是JSON数据,而不是文件名!因此,如果要从文件中解析JSON,可以编写 json_decode(file_get_contents('mailconfig.jso

我被这个问题困住了。这是我的密码:


您调用的
json\u decode
参数错误。第一个参数是JSON数据,而不是文件名!因此,如果要从文件中解析JSON,可以编写

json_decode(file_get_contents('mailconfig.json'), true);

您调用的
json\u decode
参数错误。第一个参数是JSON数据,而不是文件名!因此,如果要从文件中解析JSON,可以编写

json_decode(file_get_contents('mailconfig.json'), true);
根据文档,它不以文件名作为参数,而只是一个字符串

如果要从文件中解码JSON,需要执行以下操作:

$var = file_get_contents('mailconfig.json');
$var = json_decode($var);
$var = file_json_decode('mailconfig.json', true);
或者,如果必须经常这样做,可以将整个过程封装在一个函数中:

function file_json_decode($path, $assoc = false){
    if(file_exists($path)){
        $json = file_get_contents($path);
        $result = json_decode($json, $assoc);
    } else {
        $result = null;
    }
    return $result
}
然后这样称呼它:

$var = file_get_contents('mailconfig.json');
$var = json_decode($var);
$var = file_json_decode('mailconfig.json', true);
根据文档,它不以文件名作为参数,而只是一个字符串

如果要从文件中解码JSON,需要执行以下操作:

$var = file_get_contents('mailconfig.json');
$var = json_decode($var);
$var = file_json_decode('mailconfig.json', true);
或者,如果必须经常这样做,可以将整个过程封装在一个函数中:

function file_json_decode($path, $assoc = false){
    if(file_exists($path)){
        $json = file_get_contents($path);
        $result = json_decode($json, $assoc);
    } else {
        $result = null;
    }
    return $result
}
然后这样称呼它:

$var = file_get_contents('mailconfig.json');
$var = json_decode($var);
$var = file_json_decode('mailconfig.json', true);
嗯。很简单:)谢谢:)嗯。很简单:)谢谢:)