【问题标题】:Loop through JSON data Associatively关联地循环遍历 JSON 数据
【发布时间】:2010-04-01 22:40:49
【问题描述】:

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

感谢大家的帮助

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2020-10-19
      • 2013-02-12
      • 1970-01-01
      相关资源
      最近更新 更多