PHP-PHP5.3及以上版本中检查json格式的方法

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;
    }

 

posted on 2014-11-17 16:23  John_ABC  阅读(401)  评论(0编辑  收藏  举报

导航