簡體   English   中英

如何遍歷對象數組內的Json結果對象

[英]how to loop through Json result object inside object array

我的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非常困惑,並且不知道如何有效地遍歷多個對象。

我建議,如果您的數據是一致的,那么最好直接使用該格式,而不要遍歷所有內容來查找鍵和值。 我使用您的代碼(跳過了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("----------------  ------------------");
    }
};

我認為后者更容易理解。 當然,如果您的數據不一致,那么所有這些都是沒有意義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM