Javascript 检查json数据数组是否为空

Javascript 检查json数据数组是否为空,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,这是我的ajax代码,我需要检查json数据是否有数组? 我该怎么办? 我已经试过了,但它显示出错误的输出 $.ajax({ url: url, type: 'POST', data: {'data': data}, dataType: "json", success: function (data) { if(data) { console.log('

这是我的ajax代码,我需要检查json数据是否有数组? 我该怎么办? 我已经试过了,但它显示出错误的输出

$.ajax({
        url: url,
        type: 'POST',
        data: {'data': data},
        dataType: "json",
        success: function (data) {
          if(data)
          {
            console.log('data is there');
          }
          else
          {
            console.log('data is not there'); 
          }
这是我对ajax的回应

<?php
$json_data_array = '[]';
return $json_data_array;
?>


我如何检查?

您需要检查它的长度。条件
中的数组如果(数组)
为true,则该数组没有项

if([])){
console.log('True');

}
您需要检查它的长度。条件
中的数组如果(数组)
为true,则该数组没有项

if([])){
console.log('True');
}
使用并回显json_编码($json_数据_数组)

构建一个普通数组,然后将其转换为json,这样会更容易

<?php
$json_data_array = array();
echo json_encode($json_data_array);
?>
使用并回显json_编码($json_数据_数组)

构建一个普通数组,然后将其转换为json,这样会更容易

<?php
$json_data_array = array();
echo json_encode($json_data_array);
?>

在javascript中,空数组([])返回true。您应该检查数组。javascript空数组([])中的length

返回true。您应该检查array.length

在javascript中,您可以检查长度

  var size= myarray.length;
    if(size>=1) 
    {
       alert("not empty");
    }
    else
    {
        alert("empty");
    }
在php中,可以使用以下方法进行检查

if(!empty($myarray))
{
    echo "Not Empty";
}
else
{
    echo "Empty";
}

在javascript中,您可以检查长度

  var size= myarray.length;
    if(size>=1) 
    {
       alert("not empty");
    }
    else
    {
        alert("empty");
    }
在php中,可以使用以下方法进行检查

if(!empty($myarray))
{
    echo "Not Empty";
}
else
{
    echo "Empty";
}

首先,您需要对php的返回值进行json编码,如下所示

<?php
$json_data_array = '[]';
return json_encode($json_data_array);
?>
if(data.length > 0){
  console.log('data is there');
}else{
  console.log('data is not there'); 
}

首先,您需要对php的返回值进行json编码,如下所示

<?php
$json_data_array = '[]';
return json_encode($json_data_array);
?>
if(data.length > 0){
  console.log('data is there');
}else{
  console.log('data is not there'); 
}

您可以使用以下方法进行检查:

if( Object.prototype.toString.call( your_variable ) === '[object Array]' ) {
    alert( 'Array' );
}else{
    alert('Not Array');
}

您可以使用以下方法进行检查:

if( Object.prototype.toString.call( your_variable ) === '[object Array]' ) {
    alert( 'Array' );
}else{
    alert('Not Array');
}

LengthThank哪个选项是用于检查的好选项length或normal[]这取决于您的逻辑。如果空数组对您来说是可以的(考虑为真),则使用第一个,如果不是,则使用第二个。这将是良好的选项长度或正常[]这用于检查?取决于您的逻辑。如果空数组对您来说是可以的(考虑为真),那么使用第一个数组,如果不是,则使用第二个数组