使用jQuery附加到ID解析JSON YQL输出

时间:2012-03-27 22:19:40

标签: javascript jquery json yql

我在此网址上有一个YQL输出JSON字符串:YQL JSON

我找到了其他一些

我试图理解为什么我无法从JSON返回中获取某些项目。例如,使用jQuery,如果我想要第一个DIVs H1,我使用:

$.ajax({
       url:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.missoulaavalanche.org%2Fcurrent-advisory%2F%22%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22content%22%5D%2Fdiv%5B1%5D'&format=json",
       dataType: 'jsonp',
       jsonp: 'callback',
       jsonpCallback: 'cbfunc'
    });
    function cbfunc(data){
      var id = data.query.results.div;

      $('#table').append('<li>'+id.h1+'</li>');

  $('#table').listview('refresh');
}       

我试图从第二个div ... div 1获取一些信息,比如img alt或img src,如下所示:

function cbfunc(data){
      var id = data.query.results.div[1];

      $('#table').append('<li>'+id.img.alt+'</li>');

      $('#table').listview('refresh');
    }

我一直得到未定义或没有结果......我缺少或不了解从yql JSON列表中获取结果?

编辑:我在YQL Blog about Cache busting上看了一篇帖子......所以我在那里使用他们的建议。

编辑2:这是来自yql的JSON。我想以div img src为例,但我没有得到回报或者我得到了一个对象。我认为这将是data.query.results.div 1。img.src

我得到data.query.results.div.h1没问题:

cbfunc({
 "query": {
  "count": 1,
  "created": "2012-03-28T15:36:28Z",
  "lang": "en-US",
  "results": {
   "div": {
    "id": "content",
    "div": [
     {
      "class": "post-2491 post type-post status-publish format-standard hentry category-advisories",
      "id": "post-2491",
      "h1": "March 26, 2012 Avalanche Advisory",
      "p": {
       "class": "postmetadata alt",
       "small": {
        "br": [
         null,
         null
        ],
        "a": {
         "href": "http://www.missoulaavalanche.org/category/advisories/",
         "rel": "category tag",
         "title": "View all posts in Advisories",
         "content": "Advisories"
        },
        "content": "This entry was posted on Monday, March 26th, 2012 at 6:55 am\n Categories: \n"
       }
      },
      "div": [
       {
        "id": "danger_rating",
        "a": {
         "href": "http://www.missoulaavalanche.org/wp-content/themes/missoula-avalanche/images/ratings/avalanche_danger_scale.jpg",
         "img": {
          "alt": "Current Danger Rating is MODERATE",
          "src": "http://www.missoulaavalanche.org/wp-content/themes/missoula-avalanche/images/ratings/moderate.gif"
         }
        }
       },
       {

2 个答案:

答案 0 :(得分:2)

jsonp函数选项只是定义服务器用于jsonp的包装器的函数名称。

要访问您的数据,您需要成功回调$.ajax。您的上述代码缺少$之前的.ajax

$.ajax({
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.missoulaavalanche.org%2Fcurrent-advisory%2F%22%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22content%22%5D%2Fdiv%5B1%5D'&format=json",
    dataType: 'jsonp',
    jsonp: 'callback',
    jsonpCallback: 'cbfunc',
    success: function(data) {
        var results=data.query.results;
        /* work with results object here*/

    }
});

答案 1 :(得分:1)

如果您使用jQuery.getJSON,则比您想象的要容易。

试试这个:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.missoulaavalanche.org%2Fcurrent-advisory%2F%22%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22content%22%5D%2Fdiv%5B1%5D'&format=json",
          function(data) {
              var id = data.query.results.div;
              $('#table').append('<li>'+id.h1+'</li>');
              $('#table').listview('refresh');
          }
);