php 检测 json是否正确

直接上代码

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

猜你喜欢

转载自blog.csdn.net/liuxl57805678/article/details/102912796