JSON - 迭代嵌套数据

时间:2014-04-09 12:07:51

标签: jquery json

我有一个JSON文件,其结构如下:

{"products":[
    { "tvs":[
        {"samsung":[
            {"leds":[
                {
                 "product_id": "034567",
                 "product_name": "Samsung UA22F5100 22'' LED TV (Black)",
                 "model_no": "UA22F5100",
                 "brand": "Samsung",
                 "price": 399,
                 "screen_size": 22,
                 "screen_res": "1920 x 1080",
                 "usb": 1,
                 "hdmi": 1,
                 "screen_format": "LED",
                 "dimensions": "513.1 x 366.5 x 169.6",
                 "manuf_guarantee": "1 year"                  
                },
                {
                 "product_id": "012468",
                 "product_name": "Samsung 23F4003 23'' LED TV (Black)",
                 "model_no": "23F4003",
                 "brand": "Samsung",
                 "price": 459,
                 "screen_size": 23,
                 "screen_res": "1366 x 768 pixels",
                 "usb": 1,
                 "hdmi": 1,
                 "screen_format": "LED",
                 "dimensions": "551.9 x 368.4 x 123.4",
                 "manuf_guarantee": "1 year"                
                }
        ]},
        {"plasma":[
            {
             "product_id": "043291",
             "product_name": "Samsung 43F4100 43'' Plasma TV (Black)",
             "model_no": "43F4100",
             "brand": "Samsung",
             "price": 399,
             "screen_size": 43,
             "screen_res": "852 x 480",
             "usb": 2,
             "hdmi": 2,
             "screen_format": "Plasma",
             "dimensions": "1007.4 x 670.5 x 261.9",
             "manuf_guarantee": "1 year"                 
           },
                etc...

我需要在从ajax请求中检索结果后对此进行迭代。我需要过滤数据,例如,只获取leds'个对象。 过滤的最佳方法是什么: 我应该使用jquery's .each()还是for in循环?

我正在尝试使用.each()方法迭代它,但是我被困在我需要检查当前对象是'leds'还是'plasma'的部分。

另一方面,如果我使用for (var x in obj),我可以通过'x'访问对象的名称..

我应该使用for in循环吗?我将如何做到这一点?

2 个答案:

答案 0 :(得分:1)

假设数据包含你的json

data.each(function(index, product) {
   product.each(function(index, tv) {
      tv.each(function(index, mark) {
         mark.each(function(index, type) {
            if (type=='leds') {
               // Do whatever you want ...
            }
         });
      });
   });
});

编辑:完全使用AJAX调用

var obj = {};
obj.my_post_value = my_post_value;

var str = jQuery.param(obj);

$.ajax({
    type: "POST",
    url: 'my_url',
    dataType: 'json',
    data: str,
    success: function (data) {
        // handle your successful stuff here
        data.each(function(index, product) {
           product.each(function(index, tv) {
              tv.each(function(index, mark) {
                 mark.each(function(index, type) {
                    if (type=='leds') {
                       // Do whatever you want ...
                    }
                 });
              });
           });
        });
    },
    error: function (data) {
        // handle your unsuccessful stuff here
    }
});

答案 1 :(得分:1)

这是来自原生javascript的解决方案。

<html>
<script>
var jsonArray = {"products":[
    { "tvs":[
        {"samsung":[
            {"leds":[
                {
                 "product_id": "034567",
                 "product_name": "Samsung UA22F5100 22'' LED TV (Black)",
                 "model_no": "UA22F5100",
                 "brand": "Samsung",
                 "price": 399,
                 "screen_size": 22,
                 "screen_res": "1920 x 1080",
                 "usb": 1,
                 "hdmi": 1,
                 "screen_format": "LED",
                 "dimensions": "513.1 x 366.5 x 169.6",
                 "manuf_guarantee": "1 year"                  
                },
                {
                 "product_id": "012468",
                 "product_name": "Samsung 23F4003 23'' LED TV (Black)",
                 "model_no": "23F4003",
                 "brand": "Samsung",
                 "price": 459,
                 "screen_size": 23,
                 "screen_res": "1366 x 768 pixels",
                 "usb": 1,
                 "hdmi": 1,
                 "screen_format": "LED",
                 "dimensions": "551.9 x 368.4 x 123.4",
                 "manuf_guarantee": "1 year"                
                }
        ]},
        {"plasma":[
                {
                 "product_id": "043291",
                 "product_name": "Samsung 43F4100 43'' Plasma TV (Black)",
                 "model_no": "43F4100",
                 "brand": "Samsung",
                 "price": 399,
                 "screen_size": 43,
                 "screen_res": "852 x 480",
                 "usb": 2,
                 "hdmi": 2,
                 "screen_format": "Plasma",
                 "dimensions": "1007.4 x 670.5 x 261.9",
                 "manuf_guarantee": "1 year"                 
               }
           ]
        }
        ]
}]
}]
};      

var brand = "samsung"; //I assume that you know, how to get this based on your design
var type = "leds"; //I assume that you know, how to get this based on your design

var typeArray = eval("jsonArray.products[0].tvs[0]."+brand+"[0]."+type);
if(typeArray .length){
    for(var i=0; i<typeArray .length; i++){
        var product_id = typeArray [i].product_id;
        alert(product_id);
    }
}
</script>
<body>                       
</body>
</html>