列表与LT;>无法序列化为JSON

时间:2013-05-03 12:27:52

标签: c# asp.net-mvc json

我在ASP.NET MVC项目中使用以下C#class

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中,我有一个Action,返回类型为JsonResult,我从中返回ZoneModel个对象的列表(填充自数据库)。 Detectors属性包含数据,但是当我使用return Json(viewModel);从控制器返回列表时,序列化响应中缺少检测器列表。

为什么Detectors属性没有序列化为JSON?

1 个答案:

答案 0 :(得分:2)

只是为了澄清我的评论。需要将属性声明为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; }
}
相关问题