【问题标题】:Fetch JSON data using YQL for Facebook [closed]使用 Facebook 的 YQL 获取 JSON 数据 [关闭]
【发布时间】:2012-12-19 08:25:06
【问题描述】:

我想知道如何使用YQL 获取 JSON 数据。

这是我的 JSON 网址:

https://www.facebook.com/feeds/page.php?id=397319800348866&format=json

【问题讨论】:

标签: jquery ajax json yql


【解决方案1】:

这是一个使用 jQuery 的简单示例,它可能会对您有所帮助

$(function () {
    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql",
        dataType: "jsonp",
        success: function (data) {
            console.log(data.query.results.json);
            $.each(data.query.results.json.entries, function (i, v) {
                //console.log(data.query.results.json.entries[i]);
                $('#entries').append(data.query.results.json.entries[i].title + '<br />');
            });
        }, data: {
            q: 'select * from json where url="https://www.facebook.com/feeds/page.php?id=397319800348866&format=json"',
            format: "json"
        }
    });
});

在带有上述 url 的 console.log 中,我能够得到以下结果:

entries: Array[29]
icon: "http://www.facebook.com/favicon.ico"
link: "http://www.facebook.com/"
self: "https://www.facebook.com/feeds/page.php?id=397319800348866&format=json"
title: "Doers Inc's Facebook Wall"
updated: "2012-12-19T01:08:44-08:00"

工作示例供参考:http://jsfiddle.net/DtNxb/1/

【讨论】:

  • 只有在浏览器控制台中查看结果才会显示结果。
  • 你能纠正它与 html 输出一起使用吗?
  • +1 很好的答案!
  • 我更新了它以显示条目标题。你应该能够从那里找出其余的
  • 不要使用.title,而是使用.content 来获取完整标题以及图像。 jsFiddle
【解决方案2】:

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

试试这个:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22your_url&format=json",
          function(data) {
              var id = data.query.results.div;
              $('#table').append('<li>'+id.h1+'</li>');
              $('#table').listview('refresh');
          }
);

A simple demo here

【讨论】:

  • 我给定的网址呢?你能用我给定的网址做一个小提琴吗?
  • @sami:Steven Farley 已经做到了...
  • @arttronics:是的,如果 URL 在 URL 中包含字符串 "callback=?",则请求将被视为 JSONP。这是example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多