Search code examples
c#json.netlistjson.net

null properties when deserializing json array


i have the following json array

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

and the following C# class

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

when i try to deserialize the json array into List<AbbriviationKey> using the following code:

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

properties of each AbbriviationKey are set to null:

enter image description here


Solution

  • You have a space in the key name in your json.

    Try with this:

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