以关联方式循环访问JSON数据

时间:2010-04-01 22:40:50

标签: javascript jquery json

如何在不使用数字引用数据项的情况下遍历此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"}]

感谢大家的帮助

1 个答案:

答案 0 :(得分:3)

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

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