Javascript 验证JSON数组长度

Javascript 验证JSON数组长度,javascript,arrays,json,oracle-apex,Javascript,Arrays,Json,Oracle Apex,我试图编写一个if语句,确定getDeptJSON数组是否包含多个值。如果是,则重定向到其他页面。通过我的研究,它看起来就像getDept.length>1一样简单,但我还没能做到这一点 我的Javascript代码如下所示: $.getJSON("https://apex.oracle.com/pls/apex/mfajertest1/department/"+$x('P2_DEPT_NO').value, function(getDept) { console.log(getDep

我试图编写一个
if
语句,确定
getDept
JSON数组是否包含多个值。如果是,则重定向到其他页面。通过我的研究,它看起来就像
getDept.length>1
一样简单,但我还没能做到这一点

我的Javascript代码如下所示:

$.getJSON("https://apex.oracle.com/pls/apex/mfajertest1/department/"+$x('P2_DEPT_NO').value, function(getDept) 
{
    console.log(getDept);
    if(getDept.length == 0)
        {
                window.alert("No Department with that ID");
        }
    else if(getDept.length > 1)
        {   
                apex.navigation.redirect('f?p=71293:11');
        }
    else
        {
    $x('P2_DEPT_NAME').readOnly = false;
    $x('P2_DEPT_NAME').value=getDept.items[0].dname;
    $x('P2_DEPT_NAME').readOnly = true;
    $x('P2_DEPT_LOC').readOnly = false;
    $x('P2_DEPT_LOC').value=getDept.items[0].loc;
    $x('P2_DEPT_LOC').readOnly = true;
    $x('P2_DEPT_NO').readOnly = true;
  }
});
getDept JSON数组包含以下信息:

{
    "next": {
        "$ref": "https://apex.oracle.com/pls/apex/mfajertest1/department/%7Bid%7D?page=1"
    },
    "items": [
        {
            "deptno": 10,
            "dname": "accounting",
            "loc": "madison"
        },
        {
            "deptno": 20,
            "dname": "Finance",
            "loc": "Milwaukee"
        },
        {
            "deptno": 30,
            "dname": "IT",
            "loc": "La Crosse"
        },
        {
            "deptno": 40,
            "dname": "Purchasing",
            "loc": "Green Bay"
        },
        {
            "deptno": 10,
            "dname": "Accounting II",
            "loc": "Madison II"
        },
        {
            "deptno": 50,
            "dname": "Sports",
            "loc": "Waukasha"
        }
    ]
}

如果需要,我很乐意提供更多关于这个问题的信息

您的JSON响应实际上是一个对象,而不是数组。正在检查的数组是该对象中名为
项的属性。因此,无论您在哪里使用
getDept
,都应该使用
getDept.items

$.getJSON("https://apex.oracle.com/pls/apex/mfajertest1/department/"+$x('P2_DEPT_NO').value, function(getDept) 
{
    console.log(getDept.items);
    if(getDept.items.length == 0)
        {
                window.alert("No Department with that ID");
        }
    else if(getDept.items.length > 1)
        {   
                apex.navigation.redirect('f?p=71293:11');
        }
    else
        {
    $x('P2_DEPT_NAME').readOnly = false;
    $x('P2_DEPT_NAME').value=getDept.items[0].dname;
    $x('P2_DEPT_NAME').readOnly = true;
    $x('P2_DEPT_LOC').readOnly = false;
    $x('P2_DEPT_LOC').value=getDept.items[0].loc;
    $x('P2_DEPT_LOC').readOnly = true;
    $x('P2_DEPT_NO').readOnly = true;
  }
});

JSON响应实际上是一个对象,而不是数组。正在检查的数组是该对象中名为
项的属性。因此,无论您在哪里使用
getDept
,都应该使用
getDept.items

$.getJSON("https://apex.oracle.com/pls/apex/mfajertest1/department/"+$x('P2_DEPT_NO').value, function(getDept) 
{
    console.log(getDept.items);
    if(getDept.items.length == 0)
        {
                window.alert("No Department with that ID");
        }
    else if(getDept.items.length > 1)
        {   
                apex.navigation.redirect('f?p=71293:11');
        }
    else
        {
    $x('P2_DEPT_NAME').readOnly = false;
    $x('P2_DEPT_NAME').value=getDept.items[0].dname;
    $x('P2_DEPT_NAME').readOnly = true;
    $x('P2_DEPT_LOC').readOnly = false;
    $x('P2_DEPT_LOC').value=getDept.items[0].loc;
    $x('P2_DEPT_LOC').readOnly = true;
    $x('P2_DEPT_NO').readOnly = true;
  }
});

你能给我们看看
getDept
实际上包含什么吗?这似乎有点重要。
console.log(getDept.length)
显示了什么?你确定JSON是一个数组,不是一个对象吗。根节点是一个对象,而不是数组,因此需要查询
getDept.items
。因此,我投了反对票。@JamesWright为什么不回答这个信息,并更正代码呢?@JamesWright我可以想象,你会发现这个问题不清楚,缺少研究或没有用处。但是你发现了错误这一事实不应该成为在帖子上投票的理由,IMHO。你能告诉我们
getDept
实际上包含了什么吗?这似乎有点重要。
console.log(getDept.length)
显示了什么?你确定JSON是一个数组,不是一个对象吗。根节点是一个对象,而不是数组,因此需要查询
getDept.items
。因此,我投了反对票。@JamesWright为什么不回答这个信息,并更正代码呢?@JamesWright我可以想象,你会发现这个问题不清楚,缺少研究或没有用处。但事实上,你发现了错误不应该成为投票的理由。