0
    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"},
            ]

如果我尝试警报(响应);结果将只是 --> [object] [object]。如何遍历此对象列表中的每个元素并通过 document.write.. 打印?

4

1 回答 1

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);
}
于 2013-09-26T15:35:17.157 回答