迭代JSON对象

时间:2013-06-21 11:58:30

标签: javascript ajax json loops each

由于我对JSON和jQuery相当陌生,我正在努力迭代一个对象,我已经能够使用以下代码加载:

$.ajax({
    url: source,
    dataType: 'json',
    success: function(obj) {
        // Laat een succes-melding zien wanneer de data goed ingeladen is
        console.log("SUCCESS");
    },
    error: function(obj) {
        // Laat een error-melding zien wanneer de data niet goed ingeladen is
        alert("ERROR");
    },
    complete: function(obj) {
        console.log(obj);           
    }
});

不幸的是,JSON的结构对我来说有点复杂,但由于它是外部源(通过YQL),因此无法改变:

{
  "query": {
    "count": 1,
   "created": "2013-06-21T11:42:57Z",
    "lang": "en-US",
    "diagnostics": {
      "publiclyCallable": "true",
      "url": {
        "execution-start-time": "1",
        "execution-stop-time": "1868",
        "execution-time": "1867",
        "proxy": "DEFAULT",
        "content": "contentsource"
      },
      "user-time": "1868",
      "service-time": "1867",
      "build-version": "37740"
    },
    "results": {
      "viewentries": {
        "toplevelentries": "0",
        "viewentry": [
          {
            "date": "2013-06-11",
            "noteid": "A2FA",
            "position": "1",
            "siblings": "0",
            "unid": "15D7F7A15C0DAE",
            "entrydata": [
              {
                "columnnumber": "0",
                "name": "Datum_van_instantie",
                "text": "2013-06-11"
              },
              {
                "columnnumber": "1",
                "name": "kopregel",
                "text": "Title of newsitem 1"
              },
              {
                "columnnumber": "2",
                "name": "tussenkopje",
                "text": "Introduction text of newsitem 1"
              },
              {
                "columnnumber": "3",
                "name": "broodtekst",
                "text": "Content of newsitem 1"
              },
              {
                "columnnumber": "4",
                "name": "imagetag",
                "text": "Image URL of newsitem 1"
              }
            ]
          },
          {
            "date": "2013-05-29",
            "noteid": "A2F6",
            "position": "2",
            "siblings": "0",
            "unid": "9237CD3822ED8DD",
            "entrydata": [
              {
                "columnnumber": "0",
                "name": "Datum_van_instantie",
                "text": "2013-05-29"
              },
              {
                "columnnumber": "1",
                "name": "kopregel",
                "text": "Title of newsitem 2"
              },
              {
                "columnnumber": "2",
                "name": "tussenkopje",
                "text": "Introduction text of newsitem 2"
              },
              {
                "columnnumber": "3",
                "name": "broodtekst",
                "text": "Content of newsitem 2"
              },
              {
                "columnnumber": "4",
                "name": "imagetag",
                "text": "Image URL of newsitem 2"
              }
            ]
          }
        ]
      }
   }
  }
}

我想要完成的事情是将类似于以下内容的内容打印到我的HTML文件中的元素:

Title of newsitem 1
Title of newsitem 2

从那时起我很确定我很高兴从文件中提取其余项目。对于我正在尝试的对我来说有意义的事情,在各种obj.viewentry [0]替代品上使用$ .each循环我得到一个未定义的错误。当我试图访问更具体的例如。

$.each(obj.viewentry, function(index, value) {
    console.log(this.entrydata[0]);             
});

我得到的返回是一个jQuery错误,TypeError:e未定义。虽然我很确定最后的代码也是'jQuery for Dummies',但我今天早上一直在寻找的其他答案并没有让我更清楚。

2 个答案:

答案 0 :(得分:0)

尝试使用:

$.each(obj.query.results.viewentries.viewentry, function (index, value){
    $.each(value.entrydata, function (index, value) {
        if (value.name == "kopregel") console.log(value.text);
    });
});

这里有效:http://jsfiddle.net/8tKYd/2/

答案 1 :(得分:0)

没有理由为此使用jQuery(除了ajax部分):

for (var viewEntryKey in obj.query.results.viewentries.viewentry) {
    for (var i = 0; i < obj.query.results.viewentries.viewentry[viewEntryKey].entrydata.length; i++) {
        if (obj.query.results.viewentries.viewentry[viewEntryKey].entrydata[i].name == 'kopregel') {
            console.log(obj.query.results.viewentries.viewentry[viewEntryKey].entrydata[i].text);
        }
    }
}

输出:

  

newsitem 1的标题   newsitem 2的标题