【发布时间】:2017-08-17 18:34:46
【问题描述】:
我正在尝试根据父 ID 从下面的 json 填充子对象,但遇到一些问题,我需要您的帮助。我是 Json 的新手,所以请建议我一些解决方案,如果页面 ID 为 2,我想显示 child1,child2 child3,我正在尝试根据父 ID 从 json 下面填充子对象,但面临一些问题,我需要你的在这里帮助。我是 Json 的新手,所以请给我一些解决方案,如果页面 id 为 2,我想显示 child1,child2 child3
[
{
"id": "2",
"slug": "parent",
"title": "Parent",
"subcategories": [
{
"id": "12",
"slug": "child1",
"title": "child1"
},
{
"id": "14",
"slug": "child2",
"title": "child2"
},
{
"id": "15",
"slug": "child3",
"title": "child3"
},
{
"id": "16",
"slug": "child4",
"title": "child4"
}
]
},
{
"id": "11",
"slug": "parent2",
"title": "Parent2",
"subcategories": [
{
"id": "32",
"slug": "child1",
"title": "child1"
},
{
"id": "33",
"slug": "child2",
"title": "child3"
}
]
}
]
[
{
"id": "2",
"slug": "parent",
"title": "Parent",
"subcategories": [
{
"id": "12",
"slug": "child1",
"title": "child1"
},
{
"id": "14",
"slug": "child2",
"title": "child2"
},
{
"id": "15",
"slug": "child3",
"title": "child3"
},
{
"id": "16",
"slug": "child4",
"title": "child4"
}
]
},
{
"id": "11",
"slug": "parent2",
"title": "Parent2",
"subcategories": [
{
"id": "32",
"slug": "child1",
"title": "child1"
},
{
"id": "33",
"slug": "child2",
"title": "child3"
}
]
}
]
$.getJSON("data.json" , function(json) {
$.each(json,function(i, value){
$.each(value.subcategories, function(index, obj){
$('#list-category-slider').append('<div class="item"><a href="/' + obj.slug + '">' + obj.title + '</a></div>');
})
});
});
【问题讨论】:
-
对不起,错字,是孩子
-
你的代码对我有用。
标签: javascript jquery arrays json