View Full Version : Using javascript to extract data from a json feed

12-22-2008, 04:25 AM
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.

<!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">
<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>"
<div id="thecomments"></div>

rnd me
12-22-2008, 01:02 PM
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...

12-29-2008, 04:35 AM
Thanks. You're right.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum