1$personJSON = '{"name":"Johny Carson","title":"CTO"}';
2
3$person = json_decode($personJSON);
4
5echo $person->name; // Johny Carson
1// Checks if json
2function isJson($string) {
3 json_decode($string);
4 return json_last_error() === JSON_ERROR_NONE;
5}
6
7// example
8if (isJson($string) {
9 // Do your stuff here
10}
1/** Checks if JSON and returns decoded as an array, if not, returns false,
2but you can pass the second parameter true, if you need to return
3a string in case it's not JSON */
4function tryJsonDecode($string, $returnString = false) {
5 $arr = json_decode($string);
6 if (json_last_error() === JSON_ERROR_NONE) {
7 return $arr;
8 } else {
9 return ($returnString) ? $string : false;
10 }
11}