继续收到错误:未捕获TypeError:无法读取未定义的属性'concat'

时间:2014-09-27 09:06:41

标签: jquery json

$.when(
          $.getJSON('pathto/test/collection.json'),
          $.getJSON('pathto/test/ad.json')
        ).then(function(collection, advertised){

            var items = collection.collections.concat(advertised.advertised);
            alert(items);
        });

它发出警告信息:[object Object],success,[object Object],[object Object],success,[object Object]

两个JSON文件(URL)

{
      "advertised": [
        { "title": "Rabbit",
          "subtitle": "Nibble",
          "advertised": true
         },
        { "title": "Dog",
          "subtitle": "Woof",
          "advertised": true
        },
        { "title": "Cat",
          "subtitle": "Purr",
          "advertised": true
        }
      ]
    }

{
      "collections": [
        { "title": "Horse",
          "subtitle": "Na~~",
          "advertised": false
        },
        { "title": "Turkey",
          "subtitle": "Gobble",
          "advertised": false
        },
        { "title": "Goat",
          "subtitle": "Baaaa",
          "advertised": false
        },
        { "title": "Snake",
          "subtitle": "hissssss",
          "advertised": false
        }
      ]
}

一直试图将2个json文件中的这些数据合并为1,这样我就可以使用var项来显示标题,名称等。继续得到相同的错误。

帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

希望这是你想要实现的目标:

$.when(
    $.getJSON('collection.json'),
    $.getJSON('ad.json')
).then(function(collection, advertised){
    var items = collection[0].collections.concat(advertised[0].advertised);
    alert(JSON.stringify(items));
});

我只在警报中使用了JSON.stringify,以便您可以查看JSON对象。否则警报将只显示[object Object] ..