用jquery循环一个对象数组?

时间:2021-07-08 22:55:32

I have this script that creates a list of a JSON object. I can only get this to find and show the first object. I need some help for how to loop this so i can get all results from 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
  },

Here is the JSON im getting. Even though this question has been asked before i cannot find an answer to get this to worrk.

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

2 个解决方案

#1


0  

try this:

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");
   });
}

UPDATE:

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");
   });
}

#2


1  

Use data.list instead of data.list[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  

try this:

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");
   });
}

UPDATE:

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");
   });
}

#2


1  

Use data.list instead of data.list[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");
 });

});