如何使用jQuery访问JSON的多级对象

时间:2013-05-23 23:02:57

标签: php jquery html json

在CodeIgniter中,在PHP中为下拉列表创建类似嵌套的元素<select>我正在使用此函数:

function list_categories(&$result, $cats, $sub = ''){    // <- THIS
    foreach($cats as $cat){

        //$cat['category']->id = $sub.$cat['category']->title;
        $result[$cat['category']->id] = $sub.$cat['category']->title; // <- THIS

        if( sizeof($cat['children']) > 0 ){
            $sub2 = str_replace('&mdash;&rarr;&nbsp;', '&ndash;', $sub);
            $sub2.= '&ndash;&rarr;&nbsp;';

            list_categories($result, $cat['children'], $sub2); // <- THIS
        }
    }
}
// Make an array available outside the function
$categoryData = array(0 => '-- '.lang('FORM_SELECT_CATTOP').' --');
// Launch the function to fill out the array with nested sets
list_categories($categoryData, $categories);

生成HTML <select>列表:

categories

我想在jQuery中从检索到的JSON中做同样的事情但是找不到比使用这个jQuery函数的第一级JSON对象更深入的答案:

$.getJSON('../raw_get_categories/'+com_id_selected+'/0', function(data){
    var items = [];

    $.each(data, function(key, value, sub){
        items.push('<option id="'+ key[0] + '">' + value[0] + '</option>');
    });

    console.log(items);
});

这是我想要管理的JSON,并从PHP代码中检索相同的值:

{
    "28": {
        "category": {
            "id": "28", // <<< I need to retrieve THIS
            "pid": "0",
            "com_id": "2",
            "route_id": "59",
            "excerpt": "",
            "ordering": "1",
            "title": "Oblecenie a vybava",
            "slug": "oblecenie-a-vybava1",
            "description": "",
            "image": "clothes.png",
            "seo_title": "",
            "meta": ""
        },
        "compname": {
            "id": "2",
            "name": "E-commerce"
        },
        "children": {
            "61": {
                "category": {
                    "id": "61", // <<< I need to retrieve THIS
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "52",
                    "excerpt": "",
                    "ordering": "1",
                    "title": "Ubor na hlavu",
                    "slug": "ubor-na-hlavu1",
                    "description": "<p>Capice, Klobuky, Panamy, Baretky, Prilby, Kukly<br></p>",
                    "image": "clothes-head.png",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            },
            "30": {
                "category": {
                    "id": "30",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "53",
                    "excerpt": "",
                    "ordering": "2",
                    "title": "Bundy a vetrovky",
                    "slug": "bundy-a-vetrovky",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": {
                    "34": {
                        "category": {
                            "id": "34",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Letne",
                            "slug": "letne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    },
                    "35": {
                        "category": {
                            "id": "35",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Vsesezonne",
                            "slug": "vsesezonne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    },
                    "33": {
                        "category": {
                            "id": "33",
                            "pid": "30",
                            "com_id": "2",
                            "route_id": "0",
                            "excerpt": "",
                            "ordering": "0",
                            "title": "Zimne",
                            "slug": "zimne",
                            "description": "",
                            "image": "",
                            "seo_title": "",
                            "meta": ""
                        },
                        "compname": {
                            "id": "2",
                            "name": "E-commerce"
                        },
                        "children": [ ]
                    }
                }
            },
            "31": {
                "category": {
                    "id": "31",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "54",
                    "excerpt": "",
                    "ordering": "3",
                    "title": "Nohavice a teplaky",
                    "slug": "nohavice-a-teplaky",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            },
            "32": {
                "category": {
                    "id": "32",
                    "pid": "28",
                    "com_id": "2",
                    "route_id": "55",
                    "excerpt": "",
                    "ordering": "4",
                    "title": "Obuv",
                    "slug": "obuv",
                    "description": "",
                    "image": "",
                    "seo_title": "",
                    "meta": ""
                },
                "compname": {
                    "id": "2",
                    "name": "E-commerce"
                },
                "children": [ ]
            }
        }
    },
    "38": {
    ...
}

有人可以建议我在jQuery中使用相同的foreach语句吗?

2 个答案:

答案 0 :(得分:3)

Juste使用积分来获得更多孩子

$.getJSON('filename.php', function(data) {
    $('#List li').remove();
    $.each(data, function(index, item) {
        $('#list').append('<li>'+item.category.id+'</li>');
    });
});

答案 1 :(得分:0)

您可以这样做:

$.each(data.actions, function(entryIndex, entry) {
  var html = '<li class="top-level">' + this.action + '</li>';
});

Source