gpt4 book ai didi

javascript - 仅当数据来自循环 GET 请求时,JSON stringify 才会失败

转载 作者:行者123 更新时间:2023-12-03 01:49:48 25 4
gpt4 key购买 nike

所以当我尝试创建 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"
}
]
}

最佳答案

$.get 以异步方式执行,因此请将代码放入 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);

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

关于javascript - 仅当数据来自循环 GET 请求时,JSON stringify 才会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50472199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com