简体   繁体   中英

parsing json response and display the data inside a div. weather api

How to parse json data.i need to extract data from the json response of the value weather.main and weather.description and display them inside a div.i have posted json response and js,jquery script

 //this json response and i need to extract values of weather.main and weather.description { "coord":{ "lon":80.28, "lat":13.09 }, "weather":[ { "id":802, "main":"Clouds", "description":"scattered clouds", "icon":"03d" } ], "base":"stations", "main":{ "temp":308.15, "pressure":1004, "humidity":53, "temp_min":308.15, "temp_max":308.15 }, "visibility":7000, "wind":{ "speed":3.6, "deg":260 }, "clouds":{ "all":40 }, "dt":1464852600, "sys":{ "type":1, "id":7834, "message":0.0103, "country":"IN", "sunrise":1464826282, "sunset":1464872558 }, "id":1264527, "name":"Chennai", "cod":200 } 

  <script> function loadweather(){ var q = document.getElementById("in").value; var appid = "086a3e2bd775aac95a9b096b5233f049"; var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + q + '&appid=' + appid + '&units=metric'; $.getJSON(url, function (data) { $('.temp').html('' + data.main.temp + '&#176C')}); alert("parse2"); //how to access this json data "weather->main" $.getJSON(url, function (data) { $('.cityname').html('' + data.weather.main)}); } </script> 

How to parse json data.i need to extract data from the json response of the value weather.main and weather.description and display them inside a div.i have posted json response and js,jquery script

You can do:

var obj = JSON.parse(Your_json);
obj.weather[0].main // return clouds
obj.weather[0].description // return scattered clouds
$( "body" ).append("<div>"+obj.weather[0].main+"</div>")
$( "body" ).append("<div>"+obj.weather[0].description+"</div>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM