Here is my data file named "student.json":
[
{
"firstname": "Zack",
"lastname": "F",
"email": "zack@fire.com",
"phone": "2021231234",
"address": {
"communication": "fire, Princeton, New Jersey",
"permanent": "M"
}
},
{
"firstname": "Jersey",
"lastname": "L",
"email": "gzx95@gmail.com",
"phone": "2571548741",
"address": {
"communication": "fire, Princeton, New Jersey",
"permanent": "Y"
}
}
]
And I want to list these data. But I cannot retrieve data in the nested json, the address information. The following is my code, I tried to use eval() function to get data, but it does not work. I don't know why.
<script src="js/jquery-1.8.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function() {
getDate();
function getDate() {
var temp = '';
$.ajax({
type: "get",
url: "student.json",
dataType: "json",
success: function(res) {
var list = res;
console.log(list);
for(var $i = 0; $i < list.length; $i++) {
var obj = eval(list[$i]);
console.log(obj['address']['communication']);
temp +=
'<tr>' +
'<td>' + obj['firstname'] + '</td>' +
'<td>' + obj['lastname'] + '</td>' +
'<td>' + obj['email'] + '</td>' +
'<td>' + obj['phone'] + '</td>'+
'<td>' + obj['address']['communication'] + '</td>'+
'<td>' + obj['address']['permanent'] + '</td>'+
'</tr>';
}
$("#jsonTable tr:not(:first)").html("");
$("#jsonTable").append(temp);
}
});
}
});
</script>
Can anyone help?