[英]how to loop through Json result object inside object array
I have a json object returned from my c# code. 我的C#代码返回了一个json对象。 I am looping through the result in jquery. 我正在遍历jquery中的结果。
everything is working fine BUT this is just a sample object. 一切都很好,但是这只是一个示例对象。 actual object is way much bigger and the current method which i am using is not very clean. 实际的对象要大得多,而我正在使用的当前方法不是很干净。 could any one help me how do i loop through object inside object. 谁能帮助我如何遍历对象内部的对象。
Here is my code.. 这是我的代码。
Response JSON 响应JSON
[
{
"ProductId":7363,
"ProductName":"Brisk Waterproof Men\u0027s Jacket",
"ProductDetails":{
"ImagePath":"/assets/productimages/017/017271_BLA_MENS_BRISK_JACKET_-(10).jpg",
"ImageAltText":"BLACK:3",
"ProductSummary":"Waterproof & taped seams\r\nHighly breathable fabric\r\nDouble storm flap\r\nMultiple pockets",
"MSRP":{
100.00
},
"Price":{
65.00
}
},
"StatusCode":"Success",
"ErrorMessage":null
},
{
"ProductId":6941,
"ProductName":"Fizz Kid\u0027s Waterproof Jacket",
"ProductDetails":{
"ImagePath":"/assets/productimages/016/016792_BLA_FIZZ_KIDS_JACKET_SS13_4.jpg",
"ImageAltText":"BLACK:5",
"ProductSummary":"Waterproof & taped seams\r\nDetachable hood\r\nAdjustable hem\r\nMultiple pockets",
"MSRP":{
150.00
},
"Price":{
129.00
}
},
"StatusCode":"Success",
"ErrorMessage":null
}
]
jQuery jQuery的
$('.btnGo').on("click", function (e) {
console.log("click event fired");
var jsonData = [{
ProductId: "7363"
}, {
ProductId: "6941"
}];
$.ajax({
url: "/JsonHelper/ProductHelper.ashx",
data: JSON.stringify(jsonData),
dataType: 'json',
type: 'POST',
contentType: 'application/json',
success: function (data) {
$.each(data, function (key, value) {
console.log('Object: ' + key);
var details = value.ProductDetails;
var MSRP = value.ProductDetails.MSRP;
var price = value.ProductDetails.Price;
console.log(details);
console.log(MSRP);
console.log(price);
$('.resultJson').append("<br />");
$.each(value, function (k, v) {
$('.resultJson').append(k + ": " + v + "<br />");
if (k == "ProductDetails") {
if (details != null) {
$.each(details, function (dk, dv) {
$('.resultJson').append(dk + ": " + dv + "<br />");
});
}
}
if (k == "MSRP") {
if (MSRP != null) {
$.each(MSRP, function (mk, mv) {
$('.resultJson').append(mk + ": " + mv + "<br />");
});
}
}
if (k == "Price") {
if (price != null) {
$.each(price, function (pk, pv) {
$('.resultJson').append(pk + ": " + pv + "<br />");
});
}
}
});
$('.resultJson').append("---------------- ------------------");
});
},
error: function (data, status) {
console.log("FAILED:" + status);
}
});
});
I am very confused about $.each and dont know how to efficiently loop through mulitple objects. 我对$ .each非常困惑,并且不知道如何有效地遍历多个对象。
I'd suggest that if your data is consistent, then you're better off working directly with the format and not looping through everything to find keys and values. 我建议,如果您的数据是一致的,那么最好直接使用该格式,而不要遍历所有内容来查找键和值。 I created a Fiddle using your code (skipping the AJAX part.) and for contrast another Fiddle with the following code: 我使用您的代码(跳过了AJAX部分)创建了一个小提琴,并使用以下代码对比了另一个小提琴 :
var success = function (data) {
var product, i, len, $output = $('.resultJson');
for (i = 0, len = data.length; i < len; i++) {
product = data[i];
console.log('Object: ' + i);
var details = product.ProductDetails;
var MSRP = product.ProductDetails.MSRP;
var price = product.ProductDetails.Price;
console.log(details);
console.log(MSRP);
console.log(price);
$output.append("<br />");
$output.append("ProductId: " + product.ProductId + "<br />");
$output.append("ProductName: " + product.ProductName + "<br />");
$output.append("ProductDetails: " + "<br />");
if (details) {
$output.append("ImagePath: " + details.ImagePath + "<br />");
$output.append("ImageAltText: " + details.ImageAltText + "<br />");
$output.append("ProductSummary: " + details.ProductSummary + "<br />");
$output.append("MSRP: " + MSRP + "<br />");
$output.append("Price: " + price + "<br />");
}
$output.append("StatusCode: " + product.StatusCode + "<br />");
$output.append("ErrorMessage: " + product.ErrorMessage + "<br />");
$('.resultJson').append("---------------- ------------------");
}
};
I think the latter is much easier to understand. 我认为后者更容易理解。 Of course all this is moot if your data is less consistent. 当然,如果您的数据不一致,那么所有这些都是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.