gpt4 book ai didi

JavaScript json 返回未定义

转载 作者:行者123 更新时间:2023-12-02 16:18:20 26 4
gpt4 key购买 nike

所以我试图通过 for in 循环从 json 对象中获取一些数据,但它一直返回未定义。这是我的代码:

router.get('/', function(req, res, next) {
request('https://api.instagram.com/v1/media/popular?client_id=############', function (error, response, body) {
json = JSON.parse(body);
var popular_tags = [];

for (var key in json['data']){
tag = JSON.stringify(key['tags'])
popular_tags.push(tag)
}
console.log(popular_tags)

res.render('index', {title: body });
});
});

这是我收到的输出:

[ undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined ]

但是,当我尝试

var popular_tags = [];
popular_tags.push(json['data'][0]['tags'])
console.log(popular_tags)

它会打印出我想要的内容。有什么建议吗?

最佳答案

将行更改为

tag = JSON.stringify(json['data'][key]['tags'])

关于JavaScript json 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380778/

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