note 106512 added to function.json-decode

From: Date: Tue, 15 Nov 2011 13:38:04 +0000
Subject: note 106512 added to function.json-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-184451@lists.php.net to get a copy of this message
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". ---- Server IP: 69.147.83.197 Probable Submitter: 120.138.102.114 ---- Manual Page -- http://www.php.net/manual/en/function.json-decode.php Edit -- https://master.php.net/note/edit/106512 Del: integrated -- https://master.php.net/note/delete/106512/integrated Del: useless -- https://master.php.net/note/delete/106512/useless Del: bad code -- https://master.php.net/note/delete/106512/bad+code Del: spam -- https://master.php.net/note/delete/106512/spam Del: non-english -- https://master.php.net/note/delete/106512/non-english Del: in docs -- https://master.php.net/note/delete/106512/in+docs Del: other reasons-- https://master.php.net/note/delete/106512 Reject -- https://master.php.net/note/reject/106512 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184451) next »