繁体   English   中英

Json.net反序列化创建空对象

[英]Json.net deserialization creates null object

我正在使用json.net来序列化/反序列化字典。 问题是在反序列化时,某些对象被重新创建为null。 这仅在使用PreserveReferenceHandling时发生。 码:

        Dictionary<string, object> data = new Dictionary<string, object>();
        data["Roles"] = (from r in Roles
                        select new { Id = r.Id, Name = r.Name }).ToList();

        data["CompanyCourses"] = (from cvc in CompanyVideoCourses
                                  where cvc.Company.Id == location.Company.Id
                                  select cvc).ToList();

        data["VideoCourses"] = (from vc in VideoCourses
                                select vc).ToList();

        data["CompanyCourses"] = (from cvc in CompanyVideoCourses
                                  where cvc.Company.Id == location.Company.Id
                                  select cvc).ToList();
        data["Location"] = location;

        string output = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings {                                                                                PreserveReferencesHandling = PreserveReferencesHandling.Objects});


        Dictionary<string, object> newdata = new Dictionary<string, object>();
        newdata = JsonConvert.DeserializeObject<Dictionary<string, object>>(output,
            new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
        JArray roles = JsonConvert.DeserializeObject<dynamic>(newdata["Roles"].ToString());

        //problem 1 here
        List<VideoCourse> videoCourses = JsonConvert.DeserializeObject<List<VideoCourse>>(newdata["VideoCourses"].ToString());

        //problem 2 here
        List<CompanyVideoCourse> companyCourses = JsonConvert.DeserializeObject<List<CompanyVideoCourse>>(newdata["CompanyCourses"].ToString());
        Location location1 = JsonConvert.DeserializeObject<Location>(newdata["Location"].ToString());

问题变体1-系统具有视频课程。 每个公司可以分配多个课程。 在该测试中,将一半的可用课程分配给公司。 反序列化VideoCourses时, 分配给公司的任何课程都是空的。

问题变体2-如果我通过将行起始data["CompanyCourses"] =放在行data["VideoCourses"] =之后更改字典创建的顺序,则当执行List<CompanyVideoCourse> companyCourses = ...将执行VideoCourse属性为null(这是带有其他数据的联接表)。

问题变体1和2取决于字典的顺序。 如果一个发生,另一个则没有。

我已经检查了生成的json,所有的$ id和$ ref值看起来都不错。 如上所述,这仅是使用PreserveReferencesHandling的问题。 如果不使用字典,那么无论字典创建的顺序如何,一切都会按预期工作。

因此问题是:如何重新创建所有对象?

更新

这是产生此问题的json的示例。

{
  "$id": "1",
  "CompanyCourses": [
    {
      "$id": "2",
      "Company": {
        "$id": "3",
        "Id": 1,
        "Name": "Company0",
        "Address": "123 e main",
        "ContactName": "Joe Contact",
        "ContactPhone": "2065551212",
        "ContactEmail": "joec@contactcompany.com",
        "Maritime_Id": "19360"
      },
      "VideoCourse": {
        "$id": "4",
        "InternetLocation": "5QyZRnYt",
        "SKU": "DVD0-18",
        "FileName": "Access-Control.mp4",
        "CoverFileName": "Access-Control-Cover.png",
        "PosterFileName": "Access-Control-Poster.png",
        "VTTFileName": "Access-Control.vtt",
        "RunningTime": "00:14:00",
        "Id": 11,
        "Name": "Access Control: Threat Awareness and Prevention",
        "Description": "<p>Understand and prevent the threat of unauthorized access to your ship.</p>\r\n                             <p>Topics:</p>\r\n                             <ul>\r\n                             <li>Risk recognition</li>\r\n                             <li>Piracy and hijacking prevention</li>\r\n                             <li>The threat of unauthorized access</li>\r\n                             <li>Security and team-effort approach</li>\r\n                             <li>Controlling shipboard access in port</li>\r\n                             </ul>",
        "Repeating": false,
        "Interval": 0,
        "Keywords": "ISPS"
      },
      "Id": 1,
      "Corporate_Id": null
    }
  ],
  "Location": {
    "$id": "14",
    "Id": 1,
    "Name": "ShipatSea Location0",
    "Address": null,
    "Company": {
      "$ref": "3"
    }
  }
}

反序列化时,即使$ id $ ref指向(“ 3”)的顶部,“ Location”对象中的“ Company”属性也为空。

结果是在序列化整个字典之前先序列化字典的每个部分。 这样,当对每个字典值进行反序列化时,它就会拥有自己需要的所有信息。 因此,例如:

 data["Location"] = location;

data["Location"] = JsonConvert.SerializeObject(location, new JsonSerializerSettings { 
             PreserveReferencesHandling = PreserveReferencesHandling.Objects
         });

暂无
暂无

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

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