note 106512 deleted from function.json-decode by levim
| From: | levim@php.net | Date: | Tue, 11 Dec 2012 17:25:25 +0000 |
| Subject: | note 106512 deleted from function.json-decode by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192472@lists.php.net to get a copy of this message | ||
Note Submitter: rajeevroy1525 at gmail dot com
----
To test the a string, is json encoded or not. we can use the following function
<?php
function IsJsonString($str){
try{
$jObject = json_decode($str);
}catch(Exception $e){
return false;
}
return (is_object($jObject)) ? true : false;
}
?>
Usage :
<?php
$jArray = array("vars"=>array("a"=>"1",
"b"=>"2"));
$jString = json_encode($jArray);
if (IsJsonString($jString))
{
echo "Its Json";
}
else{
echo "It isn't Json";
}
?>
The above code will return "Its Json" and in case wrong json string it will return
"Its isn't Json".