我需要一个非常非常快的方法来检查字符串是否为JSON。我觉得这不是最好的方式:
function isJson($string) {
return ((is_string($string) &&
(is_object(json_decode($string)) ||
is_array(json_decode($string))))) ? true : false;
}
有没有表演爱好者想改进这种方法?
我需要一个非常非常快的方法来检查字符串是否为JSON。我觉得这不是最好的方式:
function isJson($string) {
return ((is_string($string) &&
(is_object(json_decode($string)) ||
is_array(json_decode($string))))) ? true : false;
}
有没有表演爱好者想改进这种方法?
当前回答
为PHP 5.2兼容性新创建的函数,如果你需要成功解码的数据:
function try_json_decode( $json, & $success = null ){
// non-strings may cause warnings
if( !is_string( $json )){
$success = false;
return $json;
}
$data = json_decode( $json );
// output arg
$success =
// non-null data: success!
$data !== null ||
// null data from 'null' json: success!
$json === 'null' ||
// null data from ' null ' json padded with whitespaces: success!
preg_match('/^\s*null\s*$/', $json );
// return decoded or original data
return $success ? $data : $json;
}
用法:
$json_or_not = ...;
$data = try_json_decode( $json_or_not, $success );
if( $success )
process_data( $data );
else what_the_hell_is_it( $data );
一些测试:
var_dump( try_json_decode( array(), $success ), $success );
// ret = array(0){}, $success == bool(false)
var_dump( try_json_decode( 123, $success ), $success );
// ret = int(123), $success == bool(false)
var_dump( try_json_decode(' ', $success ), $success );
// ret = string(6) " ", $success == bool(false)
var_dump( try_json_decode( null, $success ), $success );
// ret = NULL, $success == bool(false)
var_dump( try_json_decode('null', $success ), $success );
// ret = NULL, $success == bool(true)
var_dump( try_json_decode(' null ', $success ), $success );
// ret = NULL, $success == bool(true)
var_dump( try_json_decode(' true ', $success ), $success );
// ret = bool(true), $success == bool(true)
var_dump( try_json_decode(' "hello" ', $success ), $success );
// ret = string(5) "hello", $success == bool(true)
var_dump( try_json_decode(' {"a":123} ', $success ), $success );
// ret = object(stdClass)#2 (1) { ["a"]=> int(123) }, $success == bool(true)
其他回答
最快的方法是“可能解码”可能的JSON字符串
这真的是最快的方法吗?
如果你想解码复杂的对象或更大的数组,这是最快的解决方案! 除了速度快之外,这是唯一可以可靠地处理任何类型输入值的解决方案——在某些情况下,其他函数会抛出错误或返回不正确的结果。
如果您的JSON字符串包含较短的值(例如,字符串,数字或只有1-2个属性的对象),那么在这个SO问题中的所有解决方案都得到类似的性能。
这里有一个快速的概览和比较-您可以在链接的要点中找到测试用例。最后一列使用了这个答案的代码:
PHP version: 7.4.21
test1: json_last_error() == JSON_ERROR_NONE
test2: is_object( json_decode() )
test3: json_decode() && $res != $string
test4: preg_match()
test5: "maybe decode" approach
| test1 | test2 | test3 | test4 | test5
#0 | 0.0147 | 0.0109 ✓︎ | 0.0119 | 0.0177 | 0.0194
#1 | 0.0129 | 0.0106 | 0.0098 | - INV - | 0.0078 ✓︎
#2 | 0.0076 | 0.0075 | 0.0063 ✓︎ | 0.0083 | 0.0133
#3 | 0.0126 | 0.0105 | 0.0096 ✓︎ | - INV - | 0.0172
#4 | 0.0070 | - INV - | 0.0061 ✓︎ | 0.0141 | 0.0134
#5 | 0.0114 | - INV - | 0.0101 | 0.0075 ✓︎ | 0.0168
#6 | 0.0203 | - INV - | 0.0195 | 0.0073 ✓︎ | 0.0259
#7 | 0.0046 | - INV - | - INV - | 0.0077 | 0.0031 ✓︎
#8 | 0.0066 | - INV - | - INV - | 0.0081 | 0.0020 ✓︎
#9 | 1.0781 | - INV - | 1.0555 | 0.0998 ✓︎ | 1.0385
#10 | 0.3183 ✓︎ | 0.3246 | 0.3270 | 1.0186 | 0.3311
#11 | 0.0071 | 0.0068 | 0.0067 ✓︎ | - INV - | 0.0079
#12 | - ERR - | - ERR - | - ERR - | - ERR - | 0.0025 ✓︎
#13 | - ERR - | - ERR - | - ERR - | - ERR - | 0.0024 ✓︎
Avg | 0.1251 | 0.0618 ✓︎ | 0.1463 | 0.1321 | 0.1072
请注意,最快的解决方案会产生最不正确的结果。在所有其他解决方案中,“可能解码”方法不仅是最快的,而且是唯一具有正确结果的解决方案。
下面是完整的性能比较脚本,在那里您可以看到我用于比较的测试数据:https://gist.github.com/stracker-phil/6a80e6faedea8dab090b4bf6668ee461
“可能解码”逻辑/代码
在尝试解码JSON字符串之前,我们首先执行一些类型检查和字符串比较。这为我们提供了最佳性能,因为json_decode()可能很慢。
/**
* Returns true, when the given parameter is a valid JSON string.
*/
function is_json( $value ) {
// Numeric strings are always valid JSON.
if ( is_numeric( $value ) ) { return true; }
// A non-string value can never be a JSON string.
if ( ! is_string( $value ) ) { return false; }
// Any non-numeric JSON string must be longer than 2 characters.
if ( strlen( $value ) < 2 ) { return false; }
// "null" is valid JSON string.
if ( 'null' === $value ) { return true; }
// "true" and "false" are valid JSON strings.
if ( 'true' === $value ) { return true; }
if ( 'false' === $value ) { return true; }
// Any other JSON string has to be wrapped in {}, [] or "".
if ( '{' != $value[0] && '[' != $value[0] && '"' != $value[0] ) { return false; }
// Verify that the trailing character matches the first character.
$last_char = $value[strlen($value) -1];
if ( '{' == $value[0] && '}' != $last_char ) { return false; }
if ( '[' == $value[0] && ']' != $last_char ) { return false; }
if ( '"' == $value[0] && '"' != $last_char ) { return false; }
// See if the string contents are valid JSON.
return null !== json_decode( $value );
}
额外:使用此逻辑安全地对JSON进行双重解码
此函数使用相同的逻辑,但返回解码后的JSON对象或原始值。
我在递归解码复杂对象的解析器中使用了这个函数。一些属性可能已经被早期的迭代解码了。该函数识别此值,并且不再尝试再次对值进行双解码。
/**
* Tests, if the given $value parameter is a JSON string.
* When it is a valid JSON value, the decoded value is returned.
* When the value is no JSON value (i.e. it was decoded already), then
* the original value is returned.
*/
function get_data( $value, $as_object = false ) {
if ( is_numeric( $value ) ) { return 0 + $value; }
if ( ! is_string( $value ) ) { return $value; }
if ( strlen( $value ) < 2 ) { return $value; }
if ( 'null' === $value ) { return null; }
if ( 'true' === $value ) { return true; }
if ( 'false' === $value ) { return false; }
if ( '{' != $value[0] && '[' != $value[0] && '"' != $value[0] ) { return $value; }
$json_data = json_decode( $value, $as_object );
if ( is_null( $json_data ) ) { return $value; }
return $json_data;
}
注意:在这个SO问题中,当将一个非字符串传递给任何其他解决方案时,您将得到显著的性能下降+错误的返回值(甚至是致命的错误)。此代码是防弹和高性能的。
更新:json_validate()将在PHP 8.3中上线
FYI:
我正在研究一个RFC,在php中添加一个新函数,它能够验证—只验证json字符串,而不生成对象/数组。
为什么是只验证的函数?因为json_decode()在解析json-string时创建了一个数组/对象,这会影响正在使用的内存量;这意味着在验证json字符串时可以达到最大内存限制。
为了给你一个概念,检查这段代码performance_test_json_validate()_vs_json_decode():
在该测试中,我们可以看到新函数json_validate()使用0 MB来验证json-string,而json_decode()需要109 MB(因为它在解析时在内存中创建了一个数组/对象)。
这是目前正在进行的工作,但我发布这篇文章是因为我对你对它的看法很感兴趣(我的意思是,从技术角度来看,不是你认为值得拥有它)。
Github: https://github.com/php/php-src/pull/9399
RFC(工作进行中):https://wiki.php.net/rfc/json_validate
期待您的意见/支持。
提前谢谢你。
应该是这样的:
function isJson($string)
{
// 1. Speed up the checking & prevent exception throw when non string is passed
if (is_numeric($string) ||
!is_string($string) ||
!$string) {
return false;
}
$cleaned_str = trim($string);
if (!$cleaned_str || !in_array($cleaned_str[0], ['{', '['])) {
return false;
}
// 2. Actual checking
$str = json_decode($string);
return (json_last_error() == JSON_ERROR_NONE) && $str && $str != $string;
}
单元测试
public function testIsJson()
{
$non_json_values = [
"12",
0,
1,
12,
-1,
'',
null,
0.1,
'.',
"''",
true,
false,
[],
'""',
'[]',
' {',
' [',
];
$json_values = [
'{}',
'{"foo": "bar"}',
'[{}]',
' {}',
' {} '
];
foreach ($non_json_values as $non_json_value) {
$is_json = isJson($non_json_value);
$this->assertFalse($is_json);
}
foreach ($json_values as $json_value) {
$is_json = isJson($json_value);
$this->assertTrue($is_json);
}
}
如果有本地文件站。Json无效,缺失,或超过一个月,做一些事情。
if (!is_array(json_decode(@file_get_contents("stations.json"))) || time() > filemtime("stations.json") + (60*60*24*31)){
// The json file is invalid, missing, or is more than 1 month old
// Get a fresh version
} else {
// Up to date
}
您必须验证您的输入,以确保您传递的字符串不是空的,实际上是一个字符串。空字符串不是有效的JSON。
function is_json($string) {
return !empty($string) && is_string($string) && is_array(json_decode($string, true)) && json_last_error() == 0;
}
我认为在PHP中更重要的是确定JSON对象是否有数据,因为要使用数据,您将需要调用json_encode()或json_decode()。我建议拒绝空JSON对象,这样就不必在空数据上运行编码和解码。
function has_json_data($string) {
$array = json_decode($string, true);
return !empty($string) && is_string($string) && is_array($array) && !empty($array) && json_last_error() == 0;
}