我有一个从我的c#代码返回的json对象。我在jquery中循环结果。
一切正常但这只是一个示例对象。实际的对象要大得多,我使用的当前方法不是很干净。任何人都可以帮助我如何循环对象内的对象。
这是我的代码..
响应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
$('.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);
}
});
});
我对$ .each非常困惑,并且不知道如何有效地循环遍历多个对象。
答案 0 :(得分:1)
我建议如果你的数据是一致的,那么你最好直接使用格式而不是遍历所有内容来查找键和值。我使用您的代码(跳过AJAX部分)创建了 a Fiddle ,并使用以下代码创建了对比 another Fiddle :
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("---------------- ------------------");
}
};
我认为后者更容易理解。当然,如果您的数据不那么一致,那么所有这些都没有用。