php判断json字符串

2019-11-05

方法1:

function is_not_json($str){
   return is_null(json_decode($str));
}
#true 不是json  false 是json

方法2:判断数据是合法的json数据: (PHP版本大于5.3)

function is_json($string) { www.111cn.net
    json_decode($string);
    return (json_last_error() == JSON_ERROR_NONE);
}
#true 是json  false不是json
#json_last_error()函数返回数据编解码过程中发生的错误.

注意: json编解码所操作字符串必须是UTF8的.

{/if}