function is_json($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
json_last_error()是PHP5.3版本才有的
另外提供几种检查json格式的方法
第一种
function is_not_json($str){
return is_null(json_decode($str));
}
第二种
function analyJson($json_str) {
$json_str = str_replace('\\', '', $json_str);
$out_arr = array();
preg_match('/{.*}/', $json_str, $out_arr);
if (!empty($out_arr)) {
$result = json_decode($out_arr[0], TRUE);
} else {
return FALSE;
}
return $result;
}