0

由于我对 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 is undefined。尽管我相当确定最后一个代码太“傻瓜的 jQuery”,但我今天早上一直在寻找的其他答案并没有真正让我更加清楚。

4

2 回答 2

0

Try using this:

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

Here it is working: http://jsfiddle.net/8tKYd/2/

于 2013-06-21T12:02:56.573 回答
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的标题

于 2013-06-21T12:04:18.607 回答