在jQuery中解析多维json对象

时间:2015-10-19 14:21:39

标签: jquery json

这是对象:

{
    "358":  { "order_name": "NAME3", "parts": [
            { "part_name": "item1", "part_state": "add" },
            { "part_name": "item2", "part_state": "add" },
            { "part_name": "item3", "part_state": "remove" }
        ]
    },
    "359":  { "order_name": "NAME4"}
};

这是我的尝试,我不能通过重复$.each function来获取part_name或part_state。

$.each( orders, function ( i, order ) {
    console.log( "ORDER ID: " + i + "->" + order.order_name );

    $.each( order, function ( j, part ) {
        console.log( part );
    });
});

1 个答案:

答案 0 :(得分:3)

part是一个对象数组。您可以使用的是:

$.each(orders, function (i, order) {
    console.log("ORDER ID: " + i + "->" + order.order_name);
    if (!order.parts) return;
    $.each(order.parts, function (j, part) {
        console.log(part.part_name, part.part_state);    
    });
});