在PHP中检查字符串是否为JSON的最快方法? [英] Fastest way to check if a string is JSON in PHP?

查看:113
本文介绍了在PHP中检查字符串是否为JSON的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a really, really fast method of checking if a string is JSON or not. I feel like this is not the best way:

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

任何表演爱好者都希望改善这种方法?

Any performance enthusiasts out there want to improve this method?

推荐答案

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

这篇关于在PHP中检查字符串是否为JSON的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆