Home > Software engineering >  Deserializing array of JSONElement back to a concrete list not working when using System.Text.Json
Deserializing array of JSONElement back to a concrete list not working when using System.Text.Json

Time:12-11

I have a problem when deserializing an object. The object has a property (data) that is a list of JSONElement. I'm doing:

using var doc = JsonDocument.Parse(JsonSerializer.Serialize(result));

var e = doc.RootElement.GetProperty("data");
var data = JsonSerializer.Deserialize<List<MyItem>>(e);

The serialized result variable has the following content:

{
   "data":[
      {
         "id":245,
         "number":14,
         "name":"Test"
      }
   ],
   "totalCount":-1,
   "groupCount":-1,
   "summary":null
}

And the MyItem class is as follows:

public class MyItem
{
    public int Id { get; set; }
    public int Number { get; set; }
    public string Name { get; set; }
}

The data variable is a list with x items. However all items are empty instances.

What am I doing wrong?

CodePudding user response:

The problem is likely that your data is using lowercase property names which are not translated to the property names in your class with the default deserialization settings.

using System.Text.Json;

dynamic result = new
{
    data = new dynamic[] {
        new {
            id = 245,
            number = 14,
            name = "Test"
        }
    },
    totalCount = -1,
    groupCount = -1
};

using var doc = JsonDocument.Parse(JsonSerializer.Serialize(result));

var e = doc.RootElement.GetProperty("data");
List<MyItem> data = JsonSerializer.Deserialize<List<MyItem>>(e);

Console.WriteLine($"{data.First().Id} {data.First().Number} {data.First().Name}");

The above code won't work with your MyItem class, but try this instead:

public class MyItem
{
    [JsonPropertyName("id")]
    public int Id { get; set; }
    [JsonPropertyName("number")]
    public int Number { get; set; }
    [JsonPropertyName("name")]
    public string Name { get; set; }
}

If it works, either use the JsonPropertyName on all your properties or else consider changing your deserialization options.

  • Related