2015-09-04 195 views
1

我想通過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]); 
    } 
} 

我在控制檯中看到undefinedjson_parsed.Items;

我期待的屬性名稱將顯示在alert中,例如:Srno,AuctionName ..等等。

+0

ds是一個主題。 jQuery.parseJSON需要一個字符串作爲參數。 –

+1

當你已經有一個解析對象時,你不需要調用'parseJSON'。 – Barmar

+0

同樣的問題http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object – astroanu

回答

1

那麼,表是一個數組,開始。其次,即使你使用Table[0].Items,也沒有Items屬性,這就是爲什麼你得到「未定義」。

那麼試試這個來代替:

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

DEMO

+0

不工作:http://jsfiddle.net/fvfppe8k/ – Arbaaz

+0

@Arbaaz,我更新了我的答案,包括演示。再試一次。 – Andy

+0

'json_parsed [0];'確實做了什麼? – Arbaaz

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