Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Using javascript to extract data from a json feed

    Hi guys,

    I am new to json, trying to give up on my old xml ways and use js to extract some data from a json feed from the New York Times API.

    I've been at this for hours now, I just can't seem to get it to work. All I'm trying to do is extract the most basic of data from this feed, but I'm doing something wrong. I heard this was supposed to be easy but it would take me 2 min in xsl!

    Anyways, any help would be appreciated.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>nyt comments</title>
    <script type="text/javascript" src="http://api.nytimes.com/svc/community/v2/comments/url/exact-match.json?url=http%3A%2F%2Fdotearth.blogs.nytimes.com%2F2008%2F12%2F16%2Fa-cooler-year-on-a-warming-planet%2F&api-key=mschwjy9jpmur8f98nerbjtv"></script>
    <script type="text/javascript">
    window.onload = function() {
    	//map array
    	var nyt_comments = results.comments;
    	//div we're gonna put it all in
    	var c_div = document.getElementById("thecomments");
    	//get placehlder for the comment
    	var the_comment=""
    	//get ul started
    	c_div.innerHTML = "<ul>"
    	//loop through every element and do stuff
    	for (i=0;i < nyt_comments.length;i++) {
    				the_comment ="<li>"+nyt_comments[i].commentSequence+"</li>";
    				c_div.innerHTML += the_comment
    			}
    		c_div.innerHTML += "</ul>"
    }
    </script>
    </head>
    <body>
    	<div id="thecomments"></div>
    </body>
    </html>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    you need to use a callback.

    you cannot point to a json url through a script tag and get the content, it must be jsonp.

    if you were on the site, you could use ajax to grab the json...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    bonusball (12-29-2008)

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. You're right.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •