反序列化 json 数组时为 null 属性

null properties when deserializing json array

我有以下 json 数组

  [
    {
      "Name ": "name1",
      "Abbreviation ": "abb1"
    },
    {
      "Name ": "name2",
      "Abbreviation ": "abb2"
    }
  
  ]

及以下C#class

  public class AbbriviationKey
    {
        public string Name { get; set; }
        public string Abbreviation { get; set; }
    }

当我尝试使用以下代码将 json 数组反序列化为 List<AbbriviationKey> 时:

    string json = File.ReadAllText("jsonFile.json");
            var abbriviationKeys = JsonConvert.DeserializeObject<List<AbbriviationKey>>(json);

每个 AbbriviationKey 的属性都设置为空:

您的 json 键名中有一个 space。

试试这个:

[
    {
      "Name": "name1",
      "Abbreviation": "abb1"
    },
    {
      "Name": "name2",
      "Abbreviation": "abb2"
    }
]