指定 JavaScriptSerializer 不序列化公共属性或公共字段。无法继承此类。
命名空间: System.Web.Script.Serialization
程序集: System.Web.Extensions(在 System.Web.Extensions.dll 中)
public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } [ScriptIgnoreAttribute] public int Status { get; set; } [ScriptIgnoreAttribute] public string Remark { get; set; } }
public JsonResult GetPersonJson() { List<Person> list = new List<Person>() { new Person(){ID=, Name="AAAA", Age=18,Status=1}, new Person(){ID=, Name="BBBB", Age=,Status=1}, new Person(){ID=, Name="CCCC", Age=,Status=1}, }; return Json(list, JsonRequestBehavior.AllowGet);
}
返回的Json如下:
[{"ID":1,"Name":"AAAA","Age":18},{"ID":2,"Name":"BBBB","Age":19},{"ID":3,"Name":"CCCC","Age":20}]