I have written a JavaScript to display data from JSON object. member employeeType has two employees members; one contains an array containing 3 employee objects, each containing three members and one has 3 members which are "employee", "id" and "email". I am able to display the members if a member has more than one objects but I am unable to display members of an object if the object is alone in employees. I might be unclear to explain it but you can have a look at this fiddle here.
My JSON looks like this;
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"}}]}};
This script display the data
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;
This is how it looks in JsonViews
Now how can I display the last employee object? Please point me in the right direction and explain what wrong I am doing and what should I do.