如何在获取时控制json条目

时间:2014-06-05 08:17:07

标签: javascript jquery html ajax json

我的代码显示所有json文件条目的图像和标题及摘要,我只需要显示第一个条目的图像,标题和摘要,并显示其他条目的标题。

请告知。

<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 = "http://localhost:8080/Newfolder/test.json";
$.getJSON( dmJSON, function(data) {
   $.each(data.entries, function(i, f) {
      var tblRow = "<tr>" + "<td>" + f.image + "</td>" + "<td>" + f.title + "</td>" + "<td>" + f.summary + "</td>" + "</tr>"
       $(tblRow).appendTo("#entrydata tbody");
 });

});

});
</script>
</head>

<body>

<div class="wrapper">
<div class="profile">
<table id= "entrydata" border="1">
<thead>
        <th>ID</th>
        <th>UserName</th>
        <th>Message</th>
    <th>Location</th>
        <th>Time</th>
    </thead>
  <tbody>

   </tbody>
</table>

</div>
</div>

</body>

</html>

请更新我的更新代码:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>

$(function() {

var items = [];
var dmJSON = "http://localhost:8080/Newfolder/realaljazeera.json";
$.getJSON( dmJSON, function(data) {
   $.each(data.entries, function(i, f) {
    if (i == 0) {
        var tblRow = "<tr>" + "<td>" + "<img src="+f.Image3+"/>" + "</td>" + "<td>" + f.Title + "</td>" + "<td>" + f.Summary + "</td>" + "</tr>"
    } else {
        var tblRow = "<tr>" + "<td>" + f.Title + "</td>" + "</tr>"
    }
    $(tblRow).appendTo("#entrydata tbody");
});

});

});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id= "entrydata" border="2" style="width:680px;">
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

$.each(data.entries, function(i, f) {
    if (i == 0) {
        var tblRow = // what you want for first entry
    } else {
        var tblRow = // what you want for other entries
    }
    $(tblRow).appendTo("#entrydata tbody");
});