简体   繁体   English

序列化和反序列化复杂对象

[英]Serializing and deserializing complex objects

I am trying to serialize and deserialize a list of objects. 我正在尝试序列化和反序列化对象列表。 First I generate the list: 首先,我生成列表:

var groups = db.AssetGroupList.ToList();

foreach(var group in groups)
{
   group.AssetSubGroupList = db.AssetSubGroupList.Where(p => p.AssetGroupId == group.AssetGroupId).ToList();
   foreach(var subGroup in group.AssetSubGroupList)
   {
      subGroup.AssetList = db.AssetList.Where(p => p.AssetSubGroupId == subGroup.AssetSubGroupId).ToList();                  
      foreach(var asset in subGroup.AssetList)
      {
         asset.ReportAsset = null;// db.ReportAssetList.Where(p => p.AssetId == asset.AssetId).FirstOrDefault();
      }
   }
}

Then I serialize it and deserialize it: 然后我将其序列化并反序列化:

var json_serializer = new JavaScriptSerializer();
var json = json_serializer.Serialize(groups);
List<AssetGroup> routes_list =(List<AssetGroup>)json_serializer.DeserializeObject(json);

However I get following error: System.InvalidCastException: 'Unable to cast object of type 'System.Object[]' to type 'System.Collections.Generic.List`1[CopyToCosmosDB.AssetGroup]'.' 但是,我收到以下错误消息:System.InvalidCastException:'无法将类型为'System.Object []'的对象转换为类型为'System.Collections.Generic.List`1 [CopyToCosmosDB.AssetGroup]'。”

I have following models: 我有以下型号:

[Table("AssetGroup")]
public class AssetGroup
{
    [Key]
    public int AssetGroupId { get; set; }

    public int Code { get; set; }

    public string Name { get; set; }

    [NotMapped]
    public List<AssetSubGroup> AssetSubGroupList { get; set; }
}


public class AssetSubGroup
{
    [Key]
    public int AssetSubGroupId { get; set; }

    [InverseProperty("AssetSubGroupList")]
    public int AssetGroupId { get; set; }

    public int Code { get; set; }

    public string Name { get; set; }

    [NotMapped]
    public List<Asset> AssetList { get; set; }
   }

[Table("Asset")]
public class Asset
{
    [Key]
     public int AssetId { get; set; }

    public int Code { get; set; }

    public string Name { get; set; }

    public int AssetSubGroupId { get; set; }

    [NotMapped]
    public ReportAsset ReportAsset { get; set; }
}

[Table("ReportAsset")]
public class ReportAsset
{
    [Key]
    public int ReportAssetId { get; set; }
    public decimal Rating { get; set; }
    public int ReportId { get; set; }


    public int AssetId { get; set; }
    public string Remark { get; set; }
    public DateTime? Updated { get; set; }

    //public Asset Asset { get; set; }
}

Ohh dear. 哦,亲爱的。 Seemed there is another method Deserialize that does the task, and not DeserializeObject: 似乎还有另一种方法Deserialize执行任务,而不是DeserializeObject:

var json_serializer = new JavaScriptSerializer();
var json = json_serializer.Serialize(report);
Report routes_list =(Report)json_serializer.Deserialize<Report>(json);

Thanks for your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM