显示具有一个或多个对象的JSON数据成员

时间:2014-06-10 00:46:45

标签: javascript json

我编写了一个JavaScript来显示来自JSON对象的数据。会员 employeeType 有两位 员工 成员;一个包含一个包含3个 员工 对象的数组,每个对象包含三个成员,另一个包含3个成员,即“employee”,“id”和“email”。如果成员有多个对象,我可以显示成员但如果对象在员工中是单独的,则无法显示对象的成员。我可能不清楚解释它,但你可以看看这个fiddle here

我的JSON看起来像这样;

var data = {"employeeSearch":{"employeeType":[{"employees":[{"employee":"name1", "id":"1", "email":"name1@company.com"},{"employee":"name2", "id":"2", "email":"name2@company.com"},{"employee":"name3", "id":"3", "email":"name3@company.com"}]},{"employees":{"employee":"name4", "id":"4", "email":"name4@company.com"}}]}};

此脚本显示数据

for(var i = 0; i < data.employeeSearch.employeeType.length; i++) {
    for (var j = 0; j < data.employeeSearch.employeeType[i].employees.length; j++) {
        countItem++;
        output += "<tr><td>"+ countItem +"</td>" + 
                "<td>"+ data.employeeSearch.employeeType[i].employees[j].employee + "</td>" + 
                "<td>"+ data.employeeSearch.employeeType[i].employees[j].id +"</td>" + 
                "<td>"+ data.employeeSearch.employeeType[i].employees[j].email +"</td></tr>";
    }
}
return output;

这是它在JsonViews中的外观

enter image description here

现在如何显示最后一个员工对象?请指出正确的方向,并解释我在做什么,我应该怎么做。

JSFiddle

3 个答案:

答案 0 :(得分:0)

请注意,第二个员工不是数组而是一个对象,它应该是一个数组。

jsfiddle:http://jsfiddle.net/fish_ball/HASuY/1/

var data = {
  "employeeSearch": {
    "employeeType": [{
       "employees":[
          {"employee":"name1", "id":"1", "email":"name1@company.com"},
          {"employee":"name2", "id":"2", "email":"name2@company.com"},
          {"employee":"name3", "id":"3", "email":"name3@company.com"}
        ]},{
        // Notice the differences HERE!
        "employees":[
          {"employee":"name4", "id":"4", "email":"name4@company.com"}
        ]
    }]
  }
};

答案 1 :(得分:0)

如前所述,如果可以,请更改数据结构,以便在两种情况下都有一个数组。

如果您不能这样做并假设某个员工从未拥有length属性,您可以对其进行简单测试,然后在运行时将其转换为数组:

for(var i = 0; i < data.employeeSearch.employeeType.length; i++) {
    var employees = data.employeeSearch.employeeType[i].employees;
    if (typeof employees.length !== 'number') { // not an array? Make it one.
        employees = [employees];
    }
    for (var j = 0; j < employees.length; j++) {
        // ...
    }
}

答案 2 :(得分:0)

  

现在如何显示最后一个员工对象?请指出我正确的方向,并解释我在做什么,我该怎么做。

就像fish_ball所说,你的json格式并不普通。

您的data.employeeSearch.employeeType[0]包含一系列对象empployees,而data.employeeSearch.employeeType[1]包含一个对象employees

如何,您可以访问最后一个员工对象,如下所示:

getEmployees = function() {
    var countItem = 0;

            for (var j = 0; j < data.employeeSearch.employeeType[0].employees.length; j++) {
                countItem++;
                output += "<tr><td>"+ countItem +"</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[0].employees[j].employee + "</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[0].employees[j].id +"</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[0].employees[j].email +"</td></tr>";
            }

                            output += "<tr><td>"+ (++countItem) +"</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[1].employees.employee + "</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[1].employees.id +"</td>" + 
                            "<td>"+ data.employeeSearch.employeeType[1].employees.email +"</td></tr>";


        return output;
    }

JSFiddle Demo

就像Felix King指出的那样,empolyeeType可能并不总是只有2.如果是这种情况,那么你需要检查empolyees.lengthtypeof并迭代数组如果empolyees.length>0typeof不是number。请参阅以下示例:

getEmployees = function() {
var countItem = 0;

for (var i = 0; i < data.employeeSearch.employeeType.length; i++){

    if (data.employeeSearch.employeeType[i].employees.length > 0){

        for (var j = 0; j < data.employeeSearch.employeeType[i].employees.length;j++){
            countItem++;            
            output += "<tr><td>"+ countItem +"</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees[j].employee + "</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees[j].id +"</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees[j].email +"</td></tr>";
        }

    }else{
                        output += "<tr><td>"+ (countItem+1) +"</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees.employee + "</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees.id +"</td>" + 
                        "<td>"+ data.employeeSearch.employeeType[i].employees.email +"</td></tr>";
    }

}


    return output;
} 

Another JSFiddle Demo