1

I would like a bookmarklet that when I click on it, it goes to the JSON and grabs the "temp" from there, and puts it into an alert to tell me what the weather It with a click of a button. Is there a way to do it? Or do I have to use a different API?

This is what I've so far:

function insertReply(content) {
    document.getElementById('output').innerHTML = content;
}

var script = document.createElement('script');
script.src = 'http://api.openweathermap.org/data/2.5/find?q=australia,wa&mode=json';
document.body.appendChild(script);
xav
  • 5,452
  • 7
  • 48
  • 57
user3521680
  • 97
  • 1
  • 2
  • 7
  • Related: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling May 17 '14 at 23:13

1 Answers1

0

Sure. First, realize that that API seems to support JSONP. That is, if you provide it with a callback parameter, it will wrap the response with <callback>(...) where <callback> is the value of the callback parameter. That means that you could use this URL:

http://api.openweathermap.org/data/2.5/find?q=australia,wa&mode=json&callback=insertReply

and your insertReply function would be provided with an appropriate object. Then, if you want to grab temp out of it, just use content as an object:

document.getElementById('output').innerHTML = content.list[0].main.temp;
icktoofay
  • 126,289
  • 21
  • 250
  • 231