How handling error of json decode by try and catch

前端 未结 4 1457
栀梦
栀梦 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:40

    May be you can try, validating json_decode

    try {
      $jsonData = file_get_contents($filePath) . ']';
      $jsonObj  = json_decode($jsonData, true);
    
      if (is_null($jsonObj)) {
        throw ('Error');
      }
    } catch (Exception $e) {
      echo '{"result":"FALSE","message":"Caught exception: ' . 
        $e->getMessage() . ' ~' . $filePath . '"}';
    }
    

    Read this too

提交回复
热议问题