Php 如何处理JSON中的空数据
我需要处理一个空JSON请求,以防用户出错 这是我的密码:Php 如何处理JSON中的空数据,php,json,Php,Json,我需要处理一个空JSON请求,以防用户出错 这是我的密码: $JSON = file_get_contents($url); //Puts the JSON var into a parameter that actually does the decoding into JSON $data = json_decode($JSON,true); echo $JSON; if ($JSON === ''){ header("Location: contact2.php"); }else{
$JSON = file_get_contents($url);
//Puts the JSON var into a parameter that actually does the decoding into JSON
$data = json_decode($JSON,true);
echo $JSON;
if ($JSON === ''){
header("Location: contact2.php");
}else{
//Execute the original plan
}
下面是一系列有趣的if语句,不幸的是这些语句对我来说并不奏效:
if(is_array($data))
if($JSON === null)
if($data === null)
非常感谢您的帮助 假设您使用的是PHP>=5.3,您可以使用
json\u last\u error
来确定解码是否成功:
if(json_last_error() === JSON_ERROR_NONE) {
// decoded fine
} else {
// error decoding json
}
检查数组的
是否为空
,请参考链接
使用gettype
if ( gettype(json_decode($data))=='NULL' ){
//error handler here
}
解释为什么它不起作用。有任何错误消息吗?我对PHP比较陌生,如果我有或者知道一个带调试器的像样的IDE,我会是一个总体上更快乐的人,问题更少。据我所知,json_decode方法将其放入一个数组中。当我故意使is_数组崩溃时,我很惊讶它对我不起作用。你知道为什么当if语句为true时,我的头不会在本地重定向吗?我让if语句工作了,只是标题不会重定向回上一个论坛。感谢您的帮助。请尝试删除此行echo$JSON;在进入下一页之前,我是否需要检查论坛上的JSON数据?无法理解您的问题,请解释清楚或提供代码