如何在json API中访问某个字符串变量?

时间:2015-08-30 18:01:07

标签: javascript arrays json

我正在使用weatherunderground api,我想仅在今天访问预测。我通常使用parsed_json [] []直到我得到我需要的变量,但在这种情况下有一个数组。这是我的代码:

function findWeather() {



  jQuery(document).ready(function($) {
  $.ajax({
  url : "http://api.wunderground.com/api/c531e176f43b999d/forecast/q/CT/Old_Greenwich.json",
  dataType : "jsonp",
  success : function(parsed_json) {

  var forecasts = parsed_json['forecast']['txt_forecast']['forecastday: 0']['fcttext'];

   var forecastString = "The weather is" + forecasts + "."
   speak(" test" + forecastString);
  }
  });
});  
}
function speak(x) {
var msg = new SpeechSynthesisUtterance(x);
    window.speechSynthesis.speak(msg);
}

如果您转到该网址,您将看到整个工作表和我尝试访问的信息。我一直试图解决这个问题几个小时,并且无法找到谷歌的任何帮助。

1 个答案:

答案 0 :(得分:2)

试试这个:

parsed_json['forecast']['txt_forecast']['forecastday'][0]['fcttext'];

不知道你期望的:0做什么,但它不会取消引用数组。