Catching JSON parsing errors
Errors are a part of application development. It depends on how the developer handles them to ease the life of users. While encoding or decoding JSON it may happen that the value passed to these JSON functions is erroneous or violates the JSON rules. In such cases you should always try to catch these errors and handle them.
This recipe deals specifically with error handling for JSON functions. We will use PHP's inbuilt JSON error handling methods to detect any errors in JSON.
Note
Please note that error handling in JSON is only available in PHP versions 5.3 and higher. So make sure you have the correct version of PHP installed to use this feature.
Getting ready
Create a new folder inside the Chapter4
directory and name it Recipe3
. Also make sure you have PHP version 5.3 or higher installed.
How to do it...
Create a new PHP file
index.php
in theRecipe3
folder.Using the same JSON string as present in the previous recipe, try to convert it into an object. Then write...