相关疑难解决方法(0)

有没有办法JSON.NET-序列化List <T>的子类,它还具有额外的属性?

好的,我们正在使用Newtonsoft的JSON.NET产品,我非常喜欢.但是,我有一个简单的类结构用于层次结构位置,看起来大致像这样......

public class Location
{
    public string Name{ get; set; }
    public LocationList Locations{ get; set; }
}

// Note: LocationList is simply a subclass of a List<T>
// which then adds an IsExpanded property for use by the UI.
public class LocationList : List<Location>
{
    public bool IsExpanded{ get; set; }
}

public class RootViewModel
{
    public LocationList RootLocations{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

...当我将它们序列化为JSON时,一切都很好,除了排除了LocationList类的IsExpanded属性.只序列化列表的内容.

现在,我想象的将是一个很好的格式.它本质上是相同的东西,如果LocationList它不是一个子类,List<Location>而只是一个常规对象,具有一个名为Items类型的属性List<Location>.

{
  "Locations":
  {
    "IsExpanded": true,
    "Items": …
Run Code Online (Sandbox Code Playgroud)

.net c# serialization json jsonserializer

11
推荐指数
1
解决办法
3252
查看次数

标签 统计

.net ×1

c# ×1

json ×1

jsonserializer ×1

serialization ×1