【已解决】PHP中如何判断JSON对象或JSON数组中包含某个key键值

【背景】

折腾:

【已解决】PHP中处理JSON字符串

期间,需要去判断已经解码出来的json对象或json数组中是否包含键值access_token

 

【折腾过程】

1.搜:

php json contain key

参考:

php – how to know whether key exists in Json string – Stack Overflow

json – Correct PHP code to check if a variable exists – Stack Overflow

->

PHP: property_exists – Manual

->

搜:

array_key_exists

 

PHP array_key_exists() 函数

PHP: array_key_exists – Manual

 

即:

array_key_exists:只判断数组中是否存在某个键值

isset:不仅判断是否存在某个键值,还判断键的值是否为真

所以对于:

$json ={"user_id":null}
isset( $mydata['user_id'] 

结果是False(而不是True)。

->

Json解码后是array的话,此处就用array_key_exists去判断。

2.去试试代码:

$decodedJsonObj = json_decode($respJson);
var_dump($decodedJsonObj);
var_dump(isset("access_token", $decodedJsonObj));
var_dump(array_key_exists("access_token", $decodedJsonObj));
$decodedJsonArr = json_decode($respJson, true);
var_dump($decodedJsonArr);
var_dump(isset("access_token", $decodedJsonArr));
var_dump(array_key_exists("access_token", $decodedJsonArr));

的输出是:

空白。没有输出。

3.试试:

$decodedJsonObj = json_decode($respJson);
var_dump($decodedJsonObj);
// var_dump(isset("access_token", $decodedJsonObj));
// var_dump(array_key_exists("access_token", $decodedJsonObj));
$decodedJsonArr = json_decode($respJson, true);
var_dump($decodedJsonArr);
var_dump(isset("access_token", $decodedJsonArr));
var_dump(array_key_exists("access_token", $decodedJsonArr));

结果是:

还是没输出。

4.好像key的字符串不能用双引号?换成单引号试试

$decodedJsonObj = json_decode($respJson);
var_dump($decodedJsonObj);
// var_dump(isset("access_token", $decodedJsonObj));
// var_dump(array_key_exists("access_token", $decodedJsonObj));
$decodedJsonArr = json_decode($respJson, true);
var_dump($decodedJsonArr);
// var_dump(isset("access_token", $decodedJsonArr));
var_dump(isset('access_token', $decodedJsonArr));
// var_dump(array_key_exists("access_token", $decodedJsonArr));
var_dump(array_key_exists('access_token', $decodedJsonArr));

结果:

还是没有输出。

5.去加echo或print:

echo "before decodedJsonObj";
$decodedJsonObj = json_decode($respJson);
echo "decodedJsonObj=".$decodedJsonObj;
var_dump($decodedJsonObj);
echo "var_dump decodedJsonObj done";
// var_dump(isset("access_token", $decodedJsonObj));
// var_dump(array_key_exists("access_token", $decodedJsonObj));
$decodedJsonArr = json_decode($respJson, true);
echo "decodedJsonArr=".$decodedJsonArr;
var_dump($decodedJsonArr);
echo "var_dump decodedJsonArr done";
// var_dump(isset("access_token", $decodedJsonArr));
var_dump(isset('access_token', $decodedJsonArr));
// var_dump(array_key_exists("access_token", $decodedJsonArr));
var_dump(array_key_exists('access_token', $decodedJsonArr));

结果:

没有任何输出。

6.再试试:

echo "before decodedJsonObj";
$decodedJsonObj = json_decode($respJson);
echo "decodedJsonObj=".$decodedJsonObj;
// var_dump($decodedJsonObj);
// echo "var_dump decodedJsonObj done";
// // var_dump(isset("access_token", $decodedJsonObj));
// // var_dump(array_key_exists("access_token", $decodedJsonObj));
// $decodedJsonArr = json_decode($respJson, true);
// echo "decodedJsonArr=".$decodedJsonArr;
// var_dump($decodedJsonArr);
// echo "var_dump decodedJsonArr done";
// // var_dump(isset("access_token", $decodedJsonArr));
// var_dump(isset('access_token', $decodedJsonArr));
// // var_dump(array_key_exists("access_token", $decodedJsonArr));
// var_dump(array_key_exists('access_token', $decodedJsonArr));

结果:

输出了:

before decodedJsonObj

好像是json_decode没有执行啊。。。

所以去研究:

【已解决】PHP中的json的json_decode不工作没有任何输出

7.接着发现是:

echo打印对象,程序异常:

$decodedJsonObj = json_decode($respJson); 
echo $decodedJsonObj; //code run abnormal, not output anything

代码调用错误,程序异常:

$decodedJsonArr = json_decode($respJson, true);

var_dump(isset('access_token', $decodedJsonArr)); // lead code run abnormal -> not output anythis

期间参考:

PHP: isset – Manual

 

【总结】

最终是用如下代码:

var_dump($respJson);
$decodedJsonObj = json_decode($respJson);
switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}
// echo $decodedJsonObj; //code run abnormal, not output anything
var_dump($decodedJsonObj);
/*
object(stdClass)#2 (2) {
  ["access_token"]=>
  string(107) "hfhKCZBGAWrPkMPL2aT_z1pGBWqZNBhSjbJaW3aLgXnqDg5_BRIYb8ZV3nplUFcDzkcCBwld5T0eeVN2BqlUexkrFauoi31EcT6z3D9QZvM"
  ["expires_in"]=>
  int(7200)
}
*/
// echo "var_dump decodedJsonObj done";
// var_dump(isset("access_token", $decodedJsonObj)); // lead code run abnormal -> not output anythis
var_dump(array_key_exists("access_token", $decodedJsonObj)); //bool(true)
var_dump(array_key_exists("errcode", $decodedJsonObj)); //bool(false)
$decodedJsonArr = json_decode($respJson, true);
switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}
// echo "decodedJsonArr=".$decodedJsonArr;
var_dump($decodedJsonArr);
/*
array(2) {
  ["access_token"]=>
  string(107) "hfhKCZBGAWrPkMPL2aT_z1pGBWqZNBhSjbJaW3aLgXnqDg5_BRIYb8ZV3nplUFcDzkcCBwld5T0eeVN2BqlUexkrFauoi31EcT6z3D9QZvM"
  ["expires_in"]=>
  int(7200)
}
*/
// var_dump(isset('access_token', $decodedJsonArr)); // lead code run abnormal -> not output anythis
var_dump(array_key_exists('access_token', $decodedJsonArr)); //bool(true)
var_dump(array_key_exists('errcode', $decodedJsonArr)); //bool(false)

输出结果是:

"{"access_token":"xhYQ59oZs9xS6ehH7bRJb1Zazx46D6d01PBaA50ZUOVpvlJidykk8iuzAzFAy8Lpmn1uw29PUMXe7UXg9-Lb1k4lcUZIGYnN9e0cWKSUTpc","expires_in":7200}"
 - No errorsobject(stdClass)#2 (2) {
  ["access_token"]=>
  string(107) "xhYQ59oZs9xS6ehH7bRJb1Zazx46D6d01PBaA50ZUOVpvlJidykk8iuzAzFAy8Lpmn1uw29PUMXe7UXg9-Lb1k4lcUZIGYnN9e0cWKSUTpc"
  ["expires_in"]=>
  int(7200)
}
bool(true)
bool(false)
 - No errorsarray(2) {
  ["access_token"]=>
  string(107) "xhYQ59oZs9xS6ehH7bRJb1Zazx46D6d01PBaA50ZUOVpvlJidykk8iuzAzFAy8Lpmn1uw29PUMXe7UXg9-Lb1k4lcUZIGYnN9e0cWKSUTpc"
  ["expires_in"]=>
  int(7200)
}
bool(true)
bool(false)

2.(1)可以用array_key_exists去判断object对象或array数组中是否含有某个键;

(2)不可以用isset去判断判断object对象或array数组中是否含有某个键

->所以这样的代码:

var_dump(isset('access_token', $decodedJsonArr)); // lead code run abnormal -> not output anythis

会导致程序异常:

之前的正常的代码输出都没了;

后续正常的代码也不执行,也没输出了。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量