我正在使用以下 C# class
在我的 ASP.NET MVC 项目中:
public class ZoneModel {
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public bool LineFault { get; set; }
public bool Sprinkler { get; set; }
public int Resistance { get; set; }
public string ZoneVersion { get; set; }
List<DetectorModel> Detectors { get; set; }
}
在我的一个 Controller
中s,我有一个Action
返回类型为 JsonResult
,从中我返回 ZoneModel
的列表对象(从数据库填充)。 Detectors
属性包含数据,但是当我使用 return Json(viewModel);
从 Controller 返回列表时,序列化响应中缺少检测器列表。
为什么是Detectors
属性未序列化为 JSON?
最佳答案
只是为了澄清我的评论。属性需要声明为 Public 成员,以便通过 JSON.NET 或内置 JavaScriptSerializer 进行序列化。
public class ZoneModel {
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public bool LineFault { get; set; }
public bool Sprinkler { get; set; }
public int Resistance { get; set; }
public string ZoneVersion { get; set; }
// this property will not be serialized since it is private (by default)
List<DetectorModel> Detectors { get; set; }
}
关于c# - List<> 无法序列化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358934/