Calling Remote JSON Files via Javascript / jQuery

Not too long ago, I was asked to create a rolling delicious feed using their JSON data. It took me quite a while to realize what was going wrong – but the solution was obvious as soon as the problem became clear. If you’re going down the same path, maybe this bit of advice will help.

Firstly, you have to create a local reference of the JSON data. As I understand it, this has to do with the JSON server settings and security. In any event, it’s easy enough to do. Create a file, such as my delicious-json.php. All you need to put in it is this:


<?php
echo file_get_contents("http://feeds.delicious.com/v2/json/tag/hyperstudio");
?>

Easy peasy. Now you can reference it via jQuery, like so:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
$(document).ready(function(){
// DELICIOUS JSON -- http://feeds.delicious.com/v2/json/tag/hyperstudio
  $.getJSON('delicious-json.php', function(data){
    $.each(data, function(i, entry) {
      // Process your data here
    });
  });
});

How you use the JSON is up to you. In tomorrow’s post, I’ll describe my preferred method – cloning a model. Doing so has a lot of speed benefits, but more importantly, it separates the html from the javascript. As a consequence, any web designer can easily step in and modify the aesthetics without touching code. This facilitates the very important separation of concerns.

  • Digg
  • Sphinn
  • del.icio.us
  • RSS

Popular Posts

Alternatively, you may want to return to the Home Page to view the most recent blog posts in order. If you have any questions, I make a point of reading and responding to all comments. Thanks for joining!

  • RealTruth_LK

    Thanks


  • Thanks for the
    post, This was exactly what I needed to see.Good list, keep up the good work


  • BRAVO!wonderful
    presentation for a presenting an importing concept. brilliant work!
     

  • Really i impressed. What a wonderful presentation.Now i am happy.Thank You


  • Very helpful
    content, pleased I uncovered this website. Thanks.
     


  • I have read your
    article, I think this is very good! Simple language, concise blog! Another
    kind of style! I like!
     


  • thanks for
    energetic post
     


  • Nice information
    presented in the post, thanks for sharing such a great post.
     

  • tntu

    Nop, not so easy.
    file_get_contents like you used it is slow because it has a hard time cutting the keep alive from the remote http server where it reads the json file.

    You need to do it like this:
    $context = stream_context_create(array('http' => array('header'=>'Connection: close')));file_get_contents("http://somedomain.com/file.htm...", false, $context);

  • Face
    book does provide paid advertising option, but that's not considered one of its
    main features. If you are in a consulting business, they you are better off
    sharing your opinion writing articles and sharing views using blogs. 

  • If you sell books online focus on the price you can get for the book and the number of other copies for sale first then worry about the rank.Well good to read this article mate.Well thanks a lot for sharing.keep sharing such stuff in future too.

  • Jay

    Hi


  • Bid management is a search engine marketing tool
    that is used to achieve higher rankings on the search
    engine results page (SERP) for sites like
    Google, Yahoo, and MSN.Thanks for sharing such great article with us.I really
    liked it a lot.

  • What's up every one, here every person is sharing these kinds of know-how, therefore it’s nice to read this weblog, and I used to visit this webpage everyday.  Play Rummy

  • With the help of Google any kind of information we can get.This is an amazing.I like this blog.Thanks to share this blog with us. 

  • Great post.I really like the concept of this post and I feel that this is a very unique and rare information that you have managed to compile.

  •  Through the use of leading technologies and a leading staff of marketing experts, Market Target is able to deliver solutions that work at price points that beat the competition.

blog comments powered by Disqus