猿问
下载APP

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

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


function isJson($string) {

    return ((is_string($string) &&

            (is_object(json_decode($string)) ||

            is_array(json_decode($string))))) ? true : false;

}

任何表演爱好者都想改进这种方法吗?


慕尼黑8549860
浏览 68回答 3
3回答

largeQ

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

森栏

回答问题该函数json_last_error返回JSON编码和解码期间发生的最后一个错误。因此,检查有效JSON的最快方法是// decode the JSON data// set second parameter boolean TRUE for associative array output.$result = json_decode($json);if (json_last_error() === JSON_ERROR_NONE) {&nbsp; &nbsp; // JSON is valid}// OR this is equivalentif (json_last_error() === 0) {&nbsp; &nbsp; // JSON is valid}请注意,json_last_errorPHP> = 5.3.0仅支持。完整的程序来检查确切的错误在开发期间知道确切的错误总是好的。这是完整的程序,以检查基于PHP文档的确切错误。function json_validate($string){&nbsp; &nbsp; // decode the JSON data&nbsp; &nbsp; $result = json_decode($string);&nbsp; &nbsp; // switch and check possible JSON errors&nbsp; &nbsp; switch (json_last_error()) {&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_NONE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = ''; // JSON is valid // No error has occurred&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_DEPTH:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'The maximum stack depth has been exceeded.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_STATE_MISMATCH:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Invalid or malformed JSON.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_CTRL_CHAR:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Control character error, possibly incorrectly encoded.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_SYNTAX:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Syntax error, malformed JSON.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; // PHP >= 5.3.3&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_UTF8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; // PHP >= 5.5.0&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_RECURSION:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'One or more recursive references in the value to be encoded.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; // PHP >= 5.5.0&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_INF_OR_NAN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'One or more NAN or INF values in the value to be encoded.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case JSON_ERROR_UNSUPPORTED_TYPE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'A value of a type that cannot be encoded was given.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Unknown JSON error occured.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; if ($error !== '') {&nbsp; &nbsp; &nbsp; &nbsp; // throw the Exception or exit // or whatever :)&nbsp; &nbsp; &nbsp; &nbsp; exit($error);&nbsp; &nbsp; }&nbsp; &nbsp; // everything is OK&nbsp; &nbsp; return $result;}使用有效的JSON INPUT进行测试$json = '[{"user_id":13,"username":"stack"},{"user_id":14,"username":"over"}]';$output = json_validate($json);print_r($output);有效的输出Array(&nbsp; &nbsp; [0] => stdClass Object&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] => 13&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [username] => stack&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => stdClass Object&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] => 14&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [username] => over&nbsp; &nbsp; &nbsp; &nbsp; ))使用无效的JSON进行测试$json = '{background-color:yellow;color:#000;padding:10px;width:650px;}';$output = json_validate($json);print_r($output);无效的输出Syntax error, malformed JSON.额外注意事项(PHP> = 5.2 && PHP <5.3.0)由于json_last_errorPHP 5.2不支持,您可以检查编码或解码是否返回布尔值FALSE。这是一个例子// decode the JSON data$result = json_decode($json);if ($result === FALSE) {&nbsp; &nbsp; // JSON is invalid}希望这是有帮助的。快乐的编码!

慕姐829404

你真正需要做的就是......if (is_object(json_decode($MyJSONArray)))&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ... do something ...&nbsp; &nbsp; }此请求甚至不需要单独的功能。只需在json_decode周围包装is_object并继续。似乎这个解决方案让人们对此过于考虑。
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答