2

I am trying to read a json file using the getJSON function in jquery, I haven't done this before I would just like to get the data to appear on my webpage

here is my code

var url = "http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&callback=?" ; 
$.getJSON(url, function(res) {
    $('#result').html('<p>lon: ' + res.lon + '</p>');          

});

here is my json file

{"coord":
 {
  "lon":-0.12574,
  "lat":51.50853
 },
 "sys":
 {
  "country":"GB",
      "sunrise":1380780339,
      "sunset":1380821577
     },
 "weather":
[{
  "id":521,
      "main":"Rain",
      "description":"proximity shower rain",
      "icon":"09n"
     }],
 "base":"gdps stations",
 "main":
   {
     "temp":290.43,
     "pressure":1008,
     "humidity":88,
     "temp_min":289.15,
     "temp_max":291.48
    },
"wind":
   {
     "speed":3.1,
     "deg":140
    },
"rain":
   {
     "1h":1.65
   },
"clouds":
     {
      "all":40
     },
"dt":1380823503,
"id":2643743,
"name":"London",
"cod":200
     }
1
  • Are you getting a CORS error when you try to load it? Commented Oct 3, 2013 at 18:39

2 Answers 2

2

That would be city.coord.lon :

var url = "http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&callback=?" ; 

$.getJSON(url, function(res) {
    $('#result').html('<p>lon: ' + res.city.coord.lon + '</p>');          
});

FIDDLE

Sign up to request clarification or add additional context in comments.

3 Comments

works but what does the city pat mean I can't see it in the json file?
Then you're not looking closely enough, cause it's there.
got it example not the same as the URI
0

The following URL:

http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&callback=?

prepends a ? to the JSON output.

If you remove the callback parameter from the URL string. You should be able to read the JSON Object.

1 Comment

Not if it's a cross domain request that isn't using CORS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.