直接上代码
if (!function_exists('json_validate')) {
/**
* 检测json数据格式是否正确
* Author:刘星麟
* @param $string
* @return bool
*/
function json_validate($string)
{
if (is_string($string)) {
@json_decode($string);
return (json_last_error() === JSON_ERROR_NONE);
} else {
return false;
}
}
}
用到的函数 json_decode()
参考资料:https://secure.php.net/manual/zh/function.json-last-error.php