无法获取json数据

时间:2016-01-03 08:03:13

标签: javascript json

以下是我的JavaScript代码:

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);
        }
    });
});

我试图将maxtempi置于dailysummary之下,但无法这样做。我的代码怎么了?

2 个答案:

答案 0 :(得分:2)

您正在访问错误的媒体资源:



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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须parsed_json["history"]["dailysummary"][0]["maxtempi"]访问maxtempi

&#13;
&#13;
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);
              }
              });
            });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
&#13;
&#13;
&#13;