Javascript 使用callbackWrapper函数解析.html页面时出现问题
我试图提取类似json的结构中的所有“id”,如下所示:Javascript 使用callbackWrapper函数解析.html页面时出现问题,javascript,json,node.js,Javascript,Json,Node.js,我试图提取类似json的结构中的所有“id”,如下所示: callbackWrapper({ "scoreboard": [ { "day":"", "games":[ { "id":"2433023", "lastUpdated":"1518108600", "updatedTimestamp":"2018-02-08 11:50:45",
callbackWrapper({
"scoreboard": [
{
"day":"",
"games":[
{
"id":"2433023",
"lastUpdated":"1518108600",
"updatedTimestamp":"2018-02-08 11:50:45",
"conference":"all-conf southland northeast",
"gameState":"final",
"startDate":"2017-03-14",
"startDateDisplay":"Mar. 14",
"startTime":"6:40 PM ET",
"startTimeEpoch":"1489531200",
"currentPeriod":"",
"finalMessage":"Final",
etc....
我的nodejs文件中有以下内容:
request({
url: url,
json: true
}, function (error, response, body) {
try {
var mmSchedule = body;
console.log(mmSchedule) // PRINT THE PAGE CONTENTS
for (var i = 0; i < body["scoreboard"]["games"].length; i++) {
console.log("lenght: ", mmSchedule["scoreboard"]["games"].length); <--- DOESNT WORK!!!
}
}
}
请求({ url:url, json:true },函数(错误、响应、正文){ 试一试{ var mmSchedule=主体; console.log(mmSchedule)//打印页面内容 对于(变量i=0;i
console.log(“lenght:”,mmSchedule[“scoreboard”][“games”].length);是一个对象数组。您需要多循环一次才能循环,或者如果数组中只有一个项目,您可以只使用scoreboard
无效…文件中将有多个“id”。根据您的json结构,您将获得游戏id:body.scoreboard[0]。games[0]。id,您的id将不在day下,其in gamesError将抛出“TypeError:无法读取未定义的属性“0”您好@Learn2Code,您能告诉我您的代码您现在写了什么吗?TypeError:无法读取未定义的属性“0”获取此错误。body[“scoreboard”][0][“day”][“games”].length