如何迭代JSON

时间:2012-10-04 09:36:59

标签: jquery asp.net json

如何迭代以下列表并打印其值?

 public class DummyVersions
 {
      public List<string> Version { get; set; }
 }

 [WebMethod]
 public static DummyVersions GetDummyVersions()
 {
      DummyVersions dummyversions = new DummyVersions
      {
           Version = new List<string>
           {
                "1.1.0",
                "1.1.1",
                "1.1.2",
                "1.1.3",
                "1.1.4",
                "1.1.5",
           }
      };
      return dummyversions;
 }
 $.ajax({
      type: "POST",
      url: "ManagerBaseKit.aspx/GetDummyVersions",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (result) {
           $.each(result, function (i, val) {
                alert(); // ??? here i want to print all version one by one
           });
      }
 });

1 个答案:

答案 0 :(得分:2)

使用WebMethod你的共鸣会被ASP.NET自动转换为json,所以你可以使用下面的代码遍历你的版本:

$.each(result.d.Version, function (i, val) {
  alert(val);
});

可以是result.d.Versionresult.Version,具体取决于您的ASP.NET版本,可以在此处找到更多相关信息:A breaking change between versions of ASP.NET AJAX