Jquery,循环遍历json数组

时间:2011-05-06 13:20:24

标签: jquery arrays json loops

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

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

我可以在jquery中循环遍历此数组并输出单个键/值对吗?

2 个答案:

答案 0 :(得分:2)

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/

答案 1 :(得分:1)

绝对。假设您告诉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>