How handling error of json decode by try and catch

前端 未结 4 1449
栀梦
栀梦 2021-02-12 16:20

I am unable to handle JSON decode errors. Here is my code:

try {
    $jsonData = file_get_contents($filePath) . \']\';
           


        
4条回答
  •  隐瞒了意图╮
    2021-02-12 16:51

    Since PHP 7.3 one can use the JSON_THROW_ON_ERROR constant.

    try {
        $jsonObj = json_decode($jsonData, true, $depth=512, JSON_THROW_ON_ERROR);
    } catch (Exception $e) {
        // handle exception
    }
    

    More: https://www.php.net/manual/de/function.json-decode.php#refsect1-function.json-decode-changelog

提交回复
热议问题