javascript - how to parse json data in html page -
how parse json data,i need display each , every data inside div element. using world weather online api service access weather data
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body> <input type="text" id="in"></input> <input type="hidden" id="keys" value="api"></input> <input type="hidden" value="json" id="format"></input> <button id="go">search</button> <script> $(document).ready(function(){ $("#go").click(function(){ var apikey = $("#keys").val(); var q = $("#in").val(); var format = $("#format").val(); jquery.ajax({ url: 'http://api.openweathermap.org/data/2.5/weather?key=' + apikey + '&q=' + q + '&format=' + format, success: function(response) { var obj = json.parse(response); console.log(obj); }, }); }); }); </script> </body> </html>
example output berlin:
http://api.openweathermap.org/data/2.5/weather?key=api&q=berlin&format=json
{"coord":{"lon":13.41,"lat":52.52},"sys":{"message":0.0826,"country":"de","sunrise":1433386009,"sunset":1433445756},"weather":[{"id":800,"main":"clear","description":"sky clear","icon":"01d"}],"base":"stations","main":{"temp":290.628,"temp_min":290.628,"temp_max":290.628,"pressure":1037.43,"sea_level":1043.19,"grnd_level":1037.43,"humidity":81},"wind":{"speed":2.42,"deg":314.504},"clouds":{"all":0},"dt":1433424243,"id":2950159,"name":"berlin","cod":200}
here, json.parse(response)
return array. can access elements in array either keys or indexes.
for example, obj[0]
'0'th
element in array(or response text)'
and
obj.some_attribute_name
access element key.
Comments
Post a Comment