简体   繁体   中英

Check if string is valid JSON?

How can I check if it is a valid json before it has been decoded, like in case of an array we can check with is_array(); can we do something similar in PHP?

First decode the string with oppressor . If it gives true then it is valid else invalid. Also cane use json_last_error() . You can check by this way..

$str = 'your json string';
$test = @json_decode($str);
if($test){
   echo 'Valid';
}else{
   echo 'not valid';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM