【问题标题】:Jquery, looping over a json arrayJquery,循环一个json数组
【发布时间】:2011-05-06 13:20:24
【问题描述】:

我正在从网络服务获取以下 JSON 字符串。

[{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1},
 {"TITLE":"sss","DESCRIPTION":"sss","PORTFOLIOID":2},
 {"TITLE":"sdfsdf","DESCRIPTION":"sdfsfsdf","PORTFOLIOID":3}]

我可以在 jquery 中循环这个数组并输出单独的键/值对吗?

【问题讨论】:

标签: jquery arrays json loops


【解决方案1】:
var a = [{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1}, ....]

$(a).each(function(index)
{
   //this is the object in the array, index is the index of the object in the array
   alert(this.TITLE + ' ' this.DESCRIPTION)
});

查看 jQuery 文档了解更多信息...http://api.jquery.com/jQuery.each/

【讨论】:

    【解决方案2】:

    当然。假设您告诉 jQuery 使用 AJAX 方法将此响应评估为 JSON,您只需执行以下操作:

    <script>
    $(data).each(function(idx, obj) //this loops the array
    {
        $(obj).each(function(key, value) //this loops the attributes of the object
        {
            console.log(key + ": " + value);
        }
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-22
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多