只有当数据来自循环GET请求

时间:2018-05-22 16:11:33

标签: javascript jquery json get

因此,当我尝试创建JSON字符串时,我遇到了问题。当我用于构建将被字符串化的对象/数组的数据来自循环中的GET请求时,我只会遇到问题。

当我将实际的javascript对象发布到控制台时,它包含所有正确的信息。但无法生成有效的JSON字符串。我已经测试了这种方法(下面的例子)并且知道它有效。我得出的结论是它与GET请求有关。我尝试了一些解决方案;玩变量范围,异步流量控制,仍然没有运气。因此我在这里。我错过了什么?

全部谢谢

以下是有问题的代码:

 var start = 1518;
var end = 1522;
var eventList = {};
eventList['event'] = [];

for (var i = 0; i < (end - start); i++) {
  $.get('https://www.someaddress.com/events/index.php?view=' + (start + i), function(response) {

    var eventRep = $(response).find('h2').text();
    var categoryRep = $(response).find('span.center-name').text();
    var descriptionRep = $(response).find('div.description.col-md-9').find('p').text();

    var eventScrape = {
      "title": eventRep,
      "category": categoryRep,
      "description": descriptionRep
    };
    //alert(eventRep); 
    eventList['event'].push(eventScrape);
  });
}

var str = JSON.stringify(eventList, undefined, 4);
console.log(eventList);
console.log(str);

生成的JSON失败:

    {
    "event": []
}

这是我想要的一个工作示例:

  var eventList = {};
  eventList['event'] = [];


  for (var i = 0; i < 5; i++) {
    var eventRep = "Event title " + i;
    var categoryRep = "Event category " + i;
    var descriptionRep = "Event description " + i;

    var eventScrape = {
      "title": eventRep,
      "category": categoryRep,
      "description": descriptionRep
    };

    eventList['event'].push(eventScrape);
    //alert(i);
  }

  var str = JSON.stringify(eventList, undefined, 4);
  console.log(eventList);
  console.log(str);

示例生成的JSON:

{  
   "event":[  
      {  
         "title":"Event title 0",
         "category":"Event category 0",
         "description":"Event description 0"
      },
      {  
         "title":"Event title 1",
         "category":"Event category 1",
         "description":"Event description 1"
      },
      {  
         "title":"Event title 2",
         "category":"Event category 2",
         "description":"Event description 2"
      },
      {  
         "title":"Event title 3",
         "category":"Event category 3",
         "description":"Event description 3"
      },
      {  
         "title":"Event title 4",
         "category":"Event category 4",
         "description":"Event description 4"
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

执行

$.get是异步方式,因此请将您的代码放在get函数

解决方案代码

&#13;
&#13;
var start = 1518;
var end = 1522;
var eventList = {};
eventList['event'] = [];

for (var i = 0; i < (end - start); i++) {
  $.get('https://www.someaddress.com/events/index.php?view=' + (start + i), function(response) {

    var eventRep = $(response).find('h2').text();
    var categoryRep = $(response).find('span.center-name').text();
    var descriptionRep = $(response).find('div.description.col-md-9').find('p').text();

    var eventScrape = {
      "title": eventRep,
      "category": categoryRep,
      "description": descriptionRep
    };
    //alert(eventRep); 
    eventList['event'].push(eventScrape);

    //as $.get is async data will be loaded in async manner so you cannot access it unless it is available
    var str = JSON.stringify(eventList, undefined, 4);
    console.log(eventList);
    console.log(str);
  });
}
&#13;
&#13;
&#13;