1

I am trying to loop through the properties of JSON object, but unable to do so.

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
    }
  ]
}

Based on other similar question on stackoverflow. I tried:

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

I get undefined at json_parsed.Items; in console.

I am expecting property names to be displayed in alert eg: Srno, AuctionName .. so on.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • ds is an oject. jQuery.parseJSON needs a string as a param. – Alexandr Lazarev Sep 04 '15 at 10:17
  • 1
    You don't need to call `parseJSON` when you already have a parsed object. – Barmar Sep 04 '15 at 10:25
  • same question http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object – astroanu Sep 04 '15 at 10:49
  • That's not a "JSON object," it's just an object. JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Feb 17 '17 at 15:54

2 Answers2

1

Well, Table is an array, for a start. Second, even if you used Table[0].Items, there's no Items property, which is why you're getting "undefined".

So try this instead:

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

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

I see you have jQuery in there. The Javascript approach is good. But if you want to loop through a JSON object in jQuery, then you can try following code:

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);
});
Beroza Paul
  • 2,047
  • 1
  • 16
  • 16