php - 在 PHP 中检查字符串是否为 JSON 的最快方法?

标签 php json error-handling json-deserialization jsonresult

我需要一种非常非常快速的方法来检查字符串是否为 JSON。我觉得这不是最好的方法:

function isJson($string) {
    return ((is_string($string) &&
            (is_object(json_decode($string)) ||
            is_array(json_decode($string))))) ? true : false;
}

有没有性能爱好者想要改进这种方法?

最佳答案

function isJson($string) {
   json_decode($string);
   return json_last_error() === JSON_ERROR_NONE;
}

关于php - 在 PHP 中检查字符串是否为 JSON 的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041741/

相关文章:

javascript - AJAX : How to get the last URL in a redirect flow inside an error handler with JSONP as content-type

php - 将错误从PHP返回到AJAX(jQuery)的好方法是什么?

php - Parse.com PHP SDK 获取对象的用户关系

php - Laravel 4.1 供应商目录配置

PHP mysqli 包装器 : passing by reference with __call() and call_user_func_array()

error-handling - Gulp 4即使出现错误也要继续观察

php - Laravel 5 SwiftMailer 的发送 : how to get error code

json - 在U-SQL中解析json文件

javascript - 在第一层创建 JSON 属性键链接

java - 无法在 Gson 自定义解串器中初始化 Android.Util.Pair