如何循环JSON对象的属性?

时间:2015-09-04 10:13:30

标签: javascript jquery html json

我试图遍历JSON对象的属性,但无法这样做。

var ds={
  "Table": [
    {
      "SrNo": 1,
      "AuctionName": "test auction",
      "AuctionDescription": "auction desc",
      "Testproject": "Y",
      "State": "India",
      "City": "2",
      "CompanyName": "IIFL",
      "Commodity": "10001",
      "BaseLineSpend": "50000",
      "ContractMonths": "5",
      "Owner": "arbaaz",
      "PreviewBids": "Y",
      "PrebidEndTime": "2015-09-11T18:00:00",
      "BiddingStartTime": "2015-09-10T18:00:00",
      "FirstTimeRunTime": 10,
      "TimeBtwLotClosing": 15,
      "BidRank": "20",
      "StartOverTime": 25,
      "OverTime": 30,
      "Buffer": "Y",
      "ImproveBidBy": "PERCENTAGE",
      "TieBids": "Y",
      "ActiveObservers": "Babitha G-C140492,",
      "Observers": "Tabrez Abdul Aziz Shaikh-A185615,",
      "ProjectOwner": "Tahir - Siddiqui-C107037,Tahir Ali-C132420,",
      "Administrator": "Rabi Roy-V182597,Gagan Kondalana Poonacha-C134452,Rabindra Kumar Choubey-C139454,",
      "GUID": "200869b0-e6be-4642-95ec-97509e457d63",
      "MkrId": "C123627",
      "MkrDt": "2015-09-03T16:23:15.917",
      "IsCreated": null
    }
  ]
}

基于stackoverflow上的其他类似问题。我试过了:

 var DataSet =  jQuery.parseJSON(ds);                 
    var json_parsed = DataSet.Table;      

var items = json_parsed.Items;   // this is always undefined in console.
for (var i = 0; i < items.length; ++i) {
    console.log("Item #" + i);
    for (var name in items[i]) {
        alert(name + "=" + items[i][name]);
    }
}

我在控制台的undefined获得json_parsed.Items;

我希望在警报中显示属性名称,例如:Srno,AuctionName ..等等。

2 个答案:

答案 0 :(得分:1)

嗯,Table是一个数组,一开始。其次,即使你使用了Table[0].Items,也没有Items属性,这就是你得到“未定义”的原因。

所以试试这个:

var items = json_parsed[0];
for (var name in items) {
    alert(name + "=" + items[name]);
}

DEMO

答案 1 :(得分:0)

我看到你有jQuery。 Javascript方法很好。但是如果你想在jQuery中循环一个JSON对象,那么你可以尝试下面的代码:

var ds={
  "Table": [
    {
      "SrNo": 1,
      "AuctionName": "test auction",
      "AuctionDescription": "auction desc",
      "Testproject": "Y",
      "State": "India",
      "City": "2",
      "CompanyName": "IIFL",
      "Commodity": "10001",
      "BaseLineSpend": "50000",
      "ContractMonths": "5",
      "Owner": "arbaaz",
      "PreviewBids": "Y",
      "PrebidEndTime": "2015-09-11T18:00:00",
      "BiddingStartTime": "2015-09-10T18:00:00",
      "FirstTimeRunTime": 10,
      "TimeBtwLotClosing": 15,
      "BidRank": "20",
      "StartOverTime": 25,
      "OverTime": 30,
      "Buffer": "Y",
      "ImproveBidBy": "PERCENTAGE",
      "TieBids": "Y",
      "ActiveObservers": "Babitha G-C140492,",
      "Observers": "Tabrez Abdul Aziz Shaikh-A185615,",
      "ProjectOwner": "Tahir - Siddiqui-C107037,Tahir Ali-C132420,",
      "Administrator": "Rabi Roy-V182597,Gagan Kondalana Poonacha-C134452,Rabindra Kumar Choubey-C139454,",
      "GUID": "200869b0-e6be-4642-95ec-97509e457d63",
      "MkrId": "C123627",
      "MkrDt": "2015-09-03T16:23:15.917",
      "IsCreated": null
    }
  ]
}


var jsonData = ds.Table[0];
$.each(jsonData, function(index, value) {
   console.log( index + '=' + value);
});