时间:2021-07-01 10:21:17 帮助过:64人阅读

PHP判断是否是json格式的方法
首先要记住json_encode返回的是字符串, 而json_decode返回的是对象
判断数据不是JSON格式:
function is_not_json($str){
return is_null(json_decode($str));
}判断数据是合法的json数据: (PHP版本大于5.3)
function is_json($string) { www.gxlcms.com
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}json_last_error()函数返回数据编解码过程中发生的错误
注意: json编解码所操作字符串必须是UTF8的
例子:
/**
* 解析json串
* @param type $json_str
* @return type
*/
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;
}如果不是json则返回false。
推荐教程:PHP视频教程
以上就是PHP检测是否是json的详细内容,更多请关注Gxl网其它相关文章!