kylas
kylas

Reputation: 1455

Unable to get json data

Below is my JavaScript code:

jQuery(document).ready(function($) {
    $.ajax({
         url: "http://api.wunderground.com/api/f40719f4c7835e02/history_20060405/q/CA/San_Francisco.json",
        dataType : "jsonp",
        success : function(parsed_json) {
            var maxtempi = parsed_json['dailysummary']['maxtempi'];
            //var temp_f = parsed_json['dailysummary']['maxtempi'];
            alert("Maxtempi is" + maxtempi);
        }
    });
});

I'm trying to get the maxtempi under dailysummary, but unable to do so. Whats wrong with my code ?

Upvotes: 0

Views: 75

Answers (2)

SG_
SG_

Reputation: 1336

You have to access parsed_json["history"]["dailysummary"][0]["maxtempi"] for maxtempi.

jQuery(document).ready(function($) {
 
              $.ajax({
              url :"http://api.wunderground.com/api/f40719f4c7835e02/history_20060405/q/CA/San_Francisco.json",
              dataType : "json",
              success : function(parsed_json) {
                
              var maxtempi = parsed_json["history"]["dailysummary"][0]["maxtempi"];
              //var temp_f = parsed_json['dailysummary']['maxtempi'];
              alert("Maxtempi is " + maxtempi);
              }
              });
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

Upvotes: 0

Shanoor
Shanoor

Reputation: 13662

You're accessing the wrong property:

jQuery(document).ready(function($) {
  $.ajax({
    url: "https://api.wunderground.com/api/f40719f4c7835e02/history_20060405/q/CA/San_Francisco.json",
    dataType: "jsonp",
    success: function(parsed_json) {
      console.log(parsed_json);
      var maxtempi = parsed_json.history.dailysummary[0].maxtempi;
      //var temp_f = parsed_json['dailysummary']['maxtempi'];
      alert("Maxtempi is" + maxtempi);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Related Questions