javascript - json 无法更改 dataWithLabels

标签 javascript jquery json ajax get

请您帮忙看一下代码。我想将标题名称从“Jason”更改为 JSON 文件中写入的温度。

var dataWithLabels = [
{
   "title":"Jason",  
}];

 $.ajax({
    url: "http://api.wunderground.com/api/2b38dcff93aa3dff/conditions/q/CA/Santa_Clara.json",
    type: "GET",
    dataType: "json", 
    success: function(data) {
    for(var i=0;i<1/*dataWithLabels.length*/;i++){
       var statistic = data.current_observation;
       dataWithLabels[i]['title']= statistic.temp_c;}} 
       //wanted to change Jason to the temperature written at the JSON file.Please help.
    });

 alert(dataWithLabels[0]['title']); 

http://codepen.io/wtang2/pen/bEGQKP

这不是重复的,我正在尝试将 JSON 文件的结果替换为 dataWithLabels 对象的标题

最佳答案

因为我不知道,如果您请求的 JSON 是正确的,我只是假设它是正确的。不过,如果您想了解 Ajax 请求之后 dataWithLabels 中发生了什么,您需要稍微重写该函数:

   var dataWithLabels = [{
    "title": "Jason",
  }];

  $.ajax({
    url:     "http://api.wunderground.com/api/2b38dcff93aa3dff/conditions/q/CA/Santa_Clara.json",
    type: "GET",
    dataType: "json",
    success: function(data) {
        for (var i = 0; i < 1 /*dataWithLabels.length*/ ; i++) {
          var statistic = data.current_observation;
          dataWithLabels[i]['title'] = statistic.temp_c;
          alert(dataWithLabels[i]['title']);
        }

      }
      //wanted to change Jason to the temperature written at the JSON  file.Please help.
  });

现在,在插入 statistic.temp_c 后,dataWithLabels 的状态就会发出警报。您的代码始终会在 Ajax 请求之前提醒状态。

希望有帮助!

关于javascript - json 无法更改 dataWithLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143597/

相关文章:

jquery - 多个 jQuery Cycle 寻呼机导航

javascript - 从嵌套集模型 javascript 创建 JSON

json - 在Grails View 中渲染Json响应

javascript - EXT JS6 递归调用 Store

javascript - 递归迭代结构

javascript - react : How to switch active class in buttons group?

javascript - 使用脚本在浏览器中返回导航的事件

java - 如何制作序列号表格? "-"在中间分开

jquery - 引导箱js : Change placement of Ok Button

json - 在不使用临时结构的情况下实现 Unmarshaller