简体   繁体   中英

Can't json_decode() from file - Syntax error

I am stuck with this problem. Here is my code:

 <?php $arr = [ 'from_name' => 'Rosresurs1.ru', 'from_email' => 'team@rosresurs.net', 'reply_email' => 'reply@rosresurs.net', 'subject' => 'Вас приветствует Росресурс!', 'reply_us' => 'Вопрос нам', 'charset' => 'UTF-8', 'headers' => ['List-Unsubscribe: <mailto:support@rosresurs.net?subject=Unsubscribe>, <http://rosresurs.net/escript/unsubscribe.php?token=$token>', 'Precedence: bulk'] ]; echo 'Var dump array to encode: <br>'; var_dump($arr); //Encoding $done = json_encode($arr, JSON_UNESCAPED_UNICODE); echo 'Echo encoded array to json: <br><br>'; echo $done . "<br><br><br><br>"; //Decoding echo "Starting decoding from file: <br><br>"; $var = json_decode('mailconfig.json', true); $json_errors = array( JSON_ERROR_NONE => 'No error has occurred', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', ); echo 'Last JSON error found: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL . '<br><br>'; echo 'Var dump variable: <br>'; var_dump($var); 

And here is the output:

在此处输入图片说明

And here is JSON file, from which I tried to decode json:

{"from_name":"Rosresurs1.ru","from_email":"team@rosresurs.net","reply_email":"reply@rosresurs.net","subject":"Вас приветствует Росресурс!","reply_us":"Вопрос нам","charset":"UTF-8","headers":["List-Unsubscribe: , ","Precedence: bulk"]}

As you see my array contains UTF-8 symbols, so I have encoded them with JSON_UNESCAPED_UNICODE option. But when I try to decode(FROM FILE), it fails. But when I try to decode from encoded variable $done, it works perfectly.

My json file contains the same $done output(copied from the browser and pasted to file). json_last_error said it's a syntax error. But there is no one...

Also I pasted json string from file to online json syntax verify service and it returned "A valid JSON string".

PS I made a lot of echo helpers(see screenshot), so you can get into a problem fast(like starting encoding and decoding points).

You are calling json_decode on a wrong parameter. The first parameter is the JSON data, not a filename! So if you want to parse the JSON from a file, you may write

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

According to the docs, json_decode() does not take a filename as a parameter, only a string.

If you want to decode JSON from a file you would need to do something like this:

$var = file_get_contents('mailconfig.json');
$var = json_decode($var);

Or, if you have to do this a lot, you could wrap the whole thing in a function:

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
}

And then call it like this:

$var = file_json_decode('mailconfig.json', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM