用jquery循环一个对象数组?

时间:2014-06-12 09:00:39

标签: jquery html json

我有这个脚本创建一个JSON对象的列表。 我只能找到并显示第一个对象。我需要一些帮助来解决这个问题,以便我可以从JSON获得所有结果。

<script>

$(function() {


   var list = [];


$.getJSON('list.json', function(data) {
   $.each(data.list[1], function(i, f) {
      var tblRow = "<tr>" + "<td>" + f.title + "</td>" +
       "<td>" + f.status + "</td>" + "<td>" + f.party + "</td>" + "<td>" + f.author + "</td>" + "</tr>"
       $(tblRow).appendTo("#listdata tbody");
 });

});

});
</script>

JSON

{
"list": [
{
"fields": {
"id": "23435443543345",
"title": "02374423 233334-2334Josephine Kjellme Hi3G",
"status": "signed",
"state": "Closed",
"party": "Josephine 2222",
"partner": "Josephine 33333",
"partnercomp": "",
"author": "Team 2 Long",
"time": "2014-06-11T12:40:52Z",
"ctime": "2014-06-11T12:37:38Z",
"timeouttime": "2014-06-25T21:59:59Z",
"template": false,
"partiescount": 2,
"type": "signable",
"process": "contract",
"authentication": "standard",
"delivery": "mixed",
"deliveryMethods": [
 "email",
 "pad"
  ],
"anyinvitationundelivered": false,
"shared": false,
"file": "9222591556598841091",
"inpadqueue": false,
"deleted": false,
"reallydeleted": false,
"canperformsigning": false,
"isviewedbyauthor": false,
 "objectversion": 44
 },
 "subfields": [
 {
 "id": "2342342342342342",
 "status": "signed",
 "name": "Josephine 4444",
 "time": "2014-06-11T12:40:43Z",
 "invitationundelivered": false,
 "inpadqueue": false,
 "isauthor": false,
 "authentication": "standard",
 "delivery": "pad"
}
],
 "link": "/d/435345345345435",
 "isauthor": false,
 "docauthorcompanysameasuser": true
 },
 {
"fields": {
"id": "643553433",
"title": "0760097129 444444-3040 Maria 44444Hi3G",
"status": "signed",
"state": "Closed",
"party": "Maria Petersson",
"partner": "Maria Petersson",
"partnercomp": "",
"author": "Team Family 2",
"time": "2014-06-11T12:38:00Z",
"ctime": "2014-06-11T12:33:29Z",
"timeouttime": "2014-06-25T21:59:59Z",
"template": false,
"partiescount": 2,
"type": "signable",
"process": "contract",
"authentication": "standard",
"delivery": "mixed",
"deliveryMethods": [
 "email",
 "pad"
  ],
 "anyinvitationundelivered": false,
"shared": false,
"file": "9222591556598841083",
"inpadqueue": false,
"deleted": false,
"reallydeleted": false,
"canperformsigning": false,
"isviewedbyauthor": false,
"objectversion": 44
  },
 "subfields": [
{
 "id": "235435435345345",
 "status": "signed",
 "name": "Maria 44444",
 "time": "2014-06-11T12:37:54Z",
 "invitationundelivered": false,
 "inpadqueue": false,
 "isauthor": false,
 "authentication": "standard",
 "delivery": "pad"
  }
 ],
 "link": "/d/43354654654654654",
 "isauthor": false,
 "docauthorcompanysameasuser": true
  },

这是JSON即时通讯。 即使这个问题在我找不到答案之前已经被问到了这个问题。

2 个答案:

答案 0 :(得分:1)

使用data.list而不是data.list [1]

$.getJSON('list.json', function(data) {
   $.each(data.list, function(i, f) {
      var tblRow = "<tr>" + "<td>" + f.title + "</td>" +
       "<td>" + f.status + "</td>" + "<td>" + f.party + "</td>" + "<td>" + f.author + "</td>" + "</tr>"
       $(tblRow).appendTo("#listdata tbody");
 });

});

答案 1 :(得分:0)

试试这个:

var list = [];
$.getJSON('list.json', function(data) {
for(x=0;x<=data.list.length;x++){
     $.each(data.list[x], function(i, f) {
     var tblRow = "<tr>" + "<td>" + f.title + "</td>" +
     "<td>" + f.status + "</td>" + "<td>" + f.party + "</td>" + "<td>" + f.author + "</td>" + "</tr>"
     $(tblRow).appendTo("#listdata tbody");
   });
}

更新:

var list = [];
$.getJSON('list.json', function(data) {
for(x=0;x<=data.list.length;x++){
     $(data.list[x]).first().load(function(i, f) {
     var tblRow = "<tr>" + "<td>" + f.title + "</td>" +
     "<td>" + f.status + "</td>" + "<td>" + f.party + "</td>" + "<td>" + f.author + "</td>" + "</tr>"
     $(tblRow).appendTo("#listdata tbody");
   });
}