递归JSON jQuery添加嵌套对象

时间:2014-12-06 15:39:09

标签: jquery json nested recursive-query

我有一个大型JSON Feed,可显示单个项目和集合。我有一个查询,提取集合,并在页面上显示图像和标题。

    $.getJSON('myJSONsource', function(data) {
    var outputCol='';
    for (var i in data) {
    if (data[i].type =="collection") {
         outputCol+='<some HTML To display image>'+ data[i].title + 
    }
    document.getElementById("my_collections").innerHTML=outputCol; 

我想要做的是在集合中显示多个嵌套对象,并提取其标题和图像。我知道我需要使用递归迭代,但我无法理解它。

JSON的示例如下:

{
"_id": "5480cf2fbfd33ed3498b456f",
"noun": "comics",
"owner_id": "547f133fbfd33e801e8b4568",
"title": "Paul's Silver Age Comics",
"trailer":"5480cf2fbfd33ed3498b456e",
"type": "collection",
"nodes": [ ],
"collection": [
          {
          "_id": "5480cf60bfd33ec9498b456c",
          "noun": "comics",
          "owner_id": "547f133fbfd33e801e8b4568",
          "title": "Detective Comics",
          "trailer": [
                       [ "5480cf60bfd33ec9498b456b"]
                     ],
          "nodes": [ ],
          "collection": "5480cf2fbfd33ed3498b456f"
           },

0 个答案:

没有答案