我想通過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;
。
我期待的屬性名稱將顯示在alert中,例如:Srno,AuctionName ..等等。
ds是一個主題。 jQuery.parseJSON需要一個字符串作爲參數。 –
當你已經有一個解析對象時,你不需要調用'parseJSON'。 – Barmar
同樣的問題http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object – astroanu