2

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

l12v6
  • 25
  • 7
  • Trivial questions like these already have an answer. I suggest you better google the problem first before posting it on SO.You'll get plenty of SO's results in the search itself. – code Jun 02 '16 at 09:40
  • Possible duplicate of http://stackoverflow.com/questions/4935632/parse-json-in-javascript?rq=1 – code Jun 02 '16 at 09:41
  • data.weather[0].main will get the main data – Shwetha Jun 02 '16 at 10:01
  • oops small mistake anyway thank you @ShwethaU 54 – l12v6 Jun 02 '16 at 11:03

1 Answers1

0

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>")
Destrif
  • 2,104
  • 1
  • 14
  • 22