Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery附加到IDs解析JSON YQL输出_Javascript_Jquery_Json_Yql - Fatal编程技术网

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

Javascript 使用jQuery附加到IDs解析JSON YQL输出,javascript,jquery,json,yql,Javascript,Jquery,Json,Yql,我在这个URL上有一个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%2Fcurren

我在这个URL上有一个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');
}       

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

要访问数据,您需要在$.ajax的成功回调中执行。上面的代码缺少$before.ajax


如果你使用它,它比你想象的要容易

试试这个:

$.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');
          }
);

你好,谢谢你的回复。我肯定知道你在那里干什么。然而,我仍然没有得到div[1]的信息。例如我想获取data.query.results.div[1].img.src。。。我可以得到data.query.results.div.h1没有问题,但是下一个div中的任何内容我都有问题。感谢数据似乎并不一致。。。第一个div在您发布的示例响应中没有img.src。您可能需要微调YQL queryHi swook。谢谢你的回复。我是否会对YQL缓存和这个示例产生问题?此外,我似乎不能得到比第一个DIV更多的结果。。。和我已经有的一样。我想得到第二个div结果,并从中获取img.src。
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"
         }
        }
       },
       {
$.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*/

    }
});
$.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');
          }
);