c# - Newtonsoft 反序列化向列表添加空值

标签 c# json serialization json.net

我只是尝试使用 C# 中的 Newtonsoft JSON 库将文件中的一些 JSON 反序列化为对象列表。以下是 MeetingFile.txt 文件的内容:

[
  {
    "People": [
      {
        "FirstName": "Malcolm",
        "LastName": "Reynolds"
      },
      {
        "FirstName": "Hoban",
        "LastName": "Washburne"
      }
    ],
    "PairDate": "1/14/2016"
  }
]

下面是反序列化对象图的代码:

using (var streamReader = new StreamReader(@"C:\Temp\MeetingFile.txt"))
{
    var fileContents = streamReader.ReadToEnd();
    var meetingHistory = JsonConvert.DeserializeObject<List<PairHistoryItem>>(fileContents);
    var serialized = JsonConvert.SerializeObject(meetingHistory);
    return meetingHistory;
}

和类:

public class Person
{
    public readonly string FirstName;
    public readonly string LastName;
    public string FullName => $"{FirstName} {LastName}";

    public List<IPairHistoryItem> PairHistory;

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

public class PairHistoryItem
{
    public List<Person> People { get; }
    public DateTime PairDate { get; }

    public PairHistoryItem(Person person1, Person person2, DateTime pairDate)
    {
        People = new List<Person> {person1, person2};
        PairDate = pairDate;
    }
}

但无论出于何种原因,这就是我的反序列化代码中的序列化 变量中包含的内容:

[
  {
    "People": [
      null,
      null,
      {
        "FirstName": "Malcolm",
        "LastName": "Reynolds",
        "PairHistory": null,
        "FullName": "Malcolm Reynolds"
      },
      {
        "FirstName": "Hoban",
        "LastName": "Washburne",
        "PairHistory": null,
        "FullName": "Hoban Washburne"
      }
    ],
    "PairDate": "2016-01-14T00:00:00"
  }
]

列表开头的null是从哪里来的?据我所知,我文件中的 JSON 格式正确。我是否缺少 JSON 转换器的某些设置?

最佳答案

@Nikolai Samteladze 完美地总结了问题的原因。除了更改构造函数之外,另一种可能的解决方案是将 Json.Net 配置为在反序列化期间替换现有列表,而不是重新使用它。为此,您可以设置 ObjectCreationHandling设置为 Replace(默认为 Auto):

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ObjectCreationHandling = ObjectCreationHandling.Replace;

var meetingHistory = JsonConvert.DeserializeObject<List<PairHistoryItem>>(fileContents, settings);

但是,为了使其正常工作,您还需要为 PairHistoryItem 类中的 People 属性添加一个私有(private) setter ,并用 标记它>[JsonProperty] 属性,以便 Json.Net 可以“看到”它:

public class PairHistoryItem
{
    [JsonProperty]
    public List<Person> People { get; private set; }
    public DateTime PairDate { get; }

    public PairHistoryItem(Person person1, Person person2, DateTime pairDate)
    {
        People = new List<Person> {person1, person2};
        PairDate = pairDate;
    }
}

fiddle :https://dotnetfiddle.net/Zs5GI1

关于c# - Newtonsoft 反序列化向列表添加空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803128/

相关文章:

java - 序列化 JTextPane 的文档是否会序列化 JTextPane 的监听器?

c# - 从 WSDL 文件在 Visual Studio 中创建 Web 服务代理

c# - GiveFeedback 事件未触发

c# - 在 C# 中将时间跨度转换为日期时间

java - 不使用注释的 Jackson 多态反序列化

c++ - boost 类包装指针的序列化

c# - 如何在涉及参数时测试是否存在有效方法

javascript - 无法使用 d3.js 在 html 中加载 json 文件

javascript - 如何在javascript中处理JSON字符串错误(错误字符“)

json - 我如何检查 json 中的 key ,这是我从 RESTful api 获得的输出