HTML code:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(function() {
var entries = [];
var dmJSON = "data.json"; <!--my JSON filename-->
$.getJSON(dmJSON, function(data) {
$.each(data.entries, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.id + "</td>" + "<td>" + f.user.username + "</td>" + "<td>" + f.message + "</td>" + "<td> " + f.location + "</td>" + "<td>" + f.at + "</td>" + "</tr>"
$(tblRow).appendTo("#entrydata tbody");
});
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id="entrydata" border="1">
<thead>
<th>Clue</th>
<th>Answer</th>
<th>Status</th>
<th>Views</th>
<th>Time</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>
JSON数据:
[{
"entry1": {
"Clue": "First Clue",
"Answer": "Answer to the first clue",
"Status": "Rejected",
"Views": "10"
}
}, {
"entry2": {
"Clue": "Second Clue",
"Answer": "Answer to the second clue",
"Status": "Amazing",
"Views": "15"
}
}, {
"entry3": {
"Clue": "Third Clue",
"Answer": "Answer to the third clue",
"Status": "Spectacular",
"Views": "25"
}
}, {
"entry4": {
"Clue": "Fourth Clue",
"Answer": "Answer to the fourth clue",
"Status": "Rejected",
"Views": "4"
}
}, {
"entry5": {
"Clue": "Fifth Clue",
"Answer": "Answer to the fifth clue",
"Status": "Amazing",
"Views": "30"
}
}, {
"entry6": {
"Clue": "Sixth Clue",
"Answer": "Answer to the sixth clue",
"Status": "Spectacular",
"Views": "110"
}
}, {
"entry7": {
"Clue": "Seventh Clue",
"Answer": "Answer to the seventh clue",
"Status": "Rejected",
"Views": "7"
}
}, {
"entry8": {
"Clue": "Eigth Clue",
"Answer": "Answer to the eigth clue",
"Status": "Amazing",
"Views": "55"
}
}
}
我的JSON文件中有8个条目,我想将它存储在8个不同的表中,但我的脚本没有读取JSON数据。
还有如何动态创建表格?任何解决方案都会有所帮助。
答案 0 :(得分:0)
您正试图获得properties from object
does not exist
HTML
<div class="wrapper">
<div class="profile">
<table id= "entrydata" border="1">
<thead>
<th>Clue</th>
<th>Answer</th>
<th>Status</th>
<th>Views</th>
<th>Time</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<强> SCRIPT 强>
var dmJSON = [
{
"entry1":{
"Clue":"First Clue",
"Answer": "Answer to the first clue",
"Status": "Rejected",
"Views" : "10"
}
},
{
"entry2":{
"Clue":"Second Clue",
"Answer": "Answer to the second clue",
"Status": "Amazing",
"Views" : "15"
}
},
{
"entry3":{
"Clue":"Third Clue",
"Answer": "Answer to the third clue",
"Status": "Spectacular",
"Views" : "25"
}
},
{
"entry4":{
"Clue":"Fourth Clue",
"Answer": "Answer to the fourth clue",
"Status": "Rejected",
"Views" : "4"
}
},
{
"entry5":{
"Clue":"Fifth Clue",
"Answer": "Answer to the fifth clue",
"Status": "Amazing",
"Views" : "30"
}
},
{
"entry6":{
"Clue":"Sixth Clue",
"Answer": "Answer to the sixth clue",
"Status": "Spectacular",
"Views" : "110"
}
},
{
"entry7":{
"Clue":"Seventh Clue",
"Answer": "Answer to the seventh clue",
"Status": "Rejected",
"Views" : "7"
}
},
{
"entry8":{
"Clue":"Eigth Clue",
"Answer": "Answer to the eigth clue",
"Status": "Amazing",
"Views" : "55"
}
}
];
$.each(dmJSON, function(i, f) {
i = i+1;
var tblRow = "<tr>" + "<td>" + f['entry'+ i].Clue + "</td>" + "<td>" + f['entry'+ i].Answer + "</td>" + "<td>" + f['entry'+ i].Status + "</td>" + "<td> " +
f['entry'+ i].Views + "</td></tr>"
$(tblRow).appendTo("#entrydata tbody");
});
<强> DEMO 强>