gpt4 book ai didi

javascript - 以关联方式循环遍历 JSON 数据

转载 作者:行者123 更新时间:2023-11-30 23:47:36 24 4
gpt4 key购买 nike

如何在不通过数字引用数据项的情况下循环遍历此 json 数据。我想将它用作关联数组。到目前为止我有这个:

$.post('/controlpanel/search', { type: type, string: string }, function(data){

$.each(data, function() {

$.each(this, function(index, itemData) {

//alert(data.id) something like this
//currently returns undefined

});

});

}, 'json');

Json 代码示例:

[{"id":"1","title":"","link":"http:\/\/www.msn.com","date_added":"0000-00-00 00:00:00",
"privacy_type":"0","user_id":"8","field2":"","field3":"","bookmark_id":"70","tag":"clean"}]

感谢大家的帮助

最佳答案

由于您的元素位于第一层,因此可通过 this (当前元素)获取 id,如下所示:

$.post('/controlpanel/search', { type: type, string: string }, function(data){
$.each(data, function() {
alert(this.id);
});
}, 'json');

关于javascript - 以关联方式循环遍历 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2564124/

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