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

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

有没有表演爱好者想改进这种方法?


当前回答

更新: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

期待您的意见/支持。

提前谢谢你。

其他回答

简单的方法是检查json结果..

$result = @json_decode($json,true);
    if (is_array($result)) {
        echo 'JSON is valid';
    }else{
        echo 'JSON is not valid';
    }
function is_json($str){ 
    return json_decode($str) != null;
}

当检测到非法编码时,http://tr.php.net/manual/en/function.json-decode.php返回值为空。

2023年的今天,时代的回答:

虽然PHP 8.3仍在开发中,将提供新的内存效率高的json_validate()函数,但由于神奇的Symfony Polyfill组件,您可以在较旧的PHP版本(7.1和更新的版本)中使用它。

只需将以下包添加到您的项目:

composer require symfony/polyfill-php83

并在应用程序中使用它:

if (json_validate($data)) {
  // do sometihng
}

由于这种方法,您可以在旧的应用程序中使用新的PHP特性,并且在将来无需任何代码更改就可以迁移到PHP 8.3,因为当内置函数可用时,您的代码将自动使用它。

只需添加这个条件:

check if the type is string and then json decode <?php $subject = ['description' => '200 extra contacts','value' => '15','product_code' => 'OS_CONT12']; $subject = '{"description":"200 extra contacts","value":15,"product_code":"OS_CONT12"}'; if(gettype($subject) == 'string'){ $data = json_decode($subject, true); print_r($data); } else{ print_r("saurabh kasmble"); } ?> OUTPUT : Array ( [description] => 200 extra contacts [value] => 15 [product_code] => OS_CONT12 )

我使用的最简单和最快的方法是跟随;

$json_array = json_decode( $raw_json , true );

if( $json_array == NULL )   //check if it was invalid json string
    die ('Invalid');  // Invalid JSON error

 // you can execute some else condition over here in case of valid JSON

这是因为如果输入的字符串不是json或无效json, json_decode()将返回NULL。


验证JSON的简单函数

如果您必须在多个地方验证JSON,您总是可以使用下面的函数。

function is_valid_json( $raw_json ){
    return ( json_decode( $raw_json , true ) == NULL ) ? false : true ; // Yes! thats it.
}

在上面的函数中,如果它是一个有效的JSON,则返回true。