[英]How to loop through properties of a JSON object?
I am trying to loop through the properties of JSON object, but unable to do so. 我正在尝试遍历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
}
]
}
Based on other similar question on stackoverflow. 基于关于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;
我得到undefined
的json_parsed.Items;
in console. 在控制台中。
I am expecting property names to be displayed in alert eg: Srno, AuctionName .. so on. 我希望属性名称显示在警报中,例如:Srno,AuctionName ..等等。
Well, Table is an array, for a start. 好吧,Table是一个数组。 Second, even if you used Table[0].Items
, there's no Items property, which is why you're getting "undefined". 其次,即使您使用了Table[0].Items
,也没有Items属性,这就是为什么您得到“未定义”的原因。
So try this instead: 因此,请尝试以下操作:
var items = json_parsed[0];
for (var name in items) {
alert(name + "=" + items[name]);
}
I see you have jQuery in there. 我看到您里面有jQuery。 The Javascript approach is good. Javascript方法很好。 But if you want to loop through a JSON object in jQuery, then you can try following code: 但是,如果要在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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.