我怎样才能遍历json对象列表

时间:2013-09-26 15:29:49

标签: json

    Json Object List.
-------------------------
        var params = new Object(); 
        params.Id = personId;
        params.cls = Class;

 $.ajax({
                type: "POST",
                dataType: "json",
                data: $.toJSON(params),
                contentType: "application/json",
                url: "../OtpWebService.asmx/GetStudentDetails",

                         alert(response.d);

                }
            });
The out-put, when i do alert(response.d); is given below.
actually this is an json object returned by a above code from c#.

            [
                {"Name":"Nthal","Class":3,"SubjectName":"English "},
                {"Name":"Mishal","Class":4,"SubjectName":"Grammer"},
                {"Name":"Sanjeev","Class":3,"SubjectName":"Social"},
                {"Name":"Michal","Class":5,"SubjectName":"Gk"},
            ]
  
    

如果我尝试提醒(回复);结果只是 - > [对象] [对象]。     如何遍历此对象列表中的每个元素并通过document.write打印..?

  

1 个答案:

答案 0 :(得分:1)

以下代码段可能有所帮助:

var dataItem = [
    {"Name":"Nthal","Class":3,"SubjectName":"English "},
    {"Name":"Mishal","Class":4,"SubjectName":"Grammer"},
    {"Name":"Sanjeev","Class":3,"SubjectName":"Social"},
    {"Name":"Michal","Class":5,"SubjectName":"Gk"},
]

for(x in dataItem)
{
alert(dataItem[x].Name);
alert(dataItem[x].Class);
alert(dataItem[x].SubjectName);
}