3

I want to know how to fetch the JSON data using YQL.

This is my JSON URL:

https://www.facebook.com/feeds/page.php?id=397319800348866&format=json
arttronics
  • 9,957
  • 2
  • 26
  • 62
sami
  • 1,324
  • 3
  • 13
  • 19

2 Answers2

6

Heres a quick example using jQuery, it might help you out

$(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"
        }
    });
});

Inside the console.log with the above url I was able to get back the following results:

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"

Working example for reference: http://jsfiddle.net/DtNxb/1/

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
2

It's easier than you think if you use jQuery.getJSON.

Try this:

$.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

palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • what about my given url ? can you make a fiddle using my given url ? – sami Dec 19 '12 at 09:20
  • @sami: **Steven Farley** has already done that... – palaѕн Dec 19 '12 at 09:23
  • 1
    @arttronics: Yes, if the URL includes the string `"callback=?"` in the URL, the request is treated as JSONP instead. Here's an [example](http://www.tbogard.com/2008/05/31/tutorial-how-to-jsonp-with-jquery-using-getjson-or-ajax/) – palaѕн Dec 19 '12 at 09:29