Home > Mobile >  ReadFromJsonAsync<T>() method does not serialize 2 level nested classes
ReadFromJsonAsync<T>() method does not serialize 2 level nested classes

Time:12-10

I am using .aspnetcore 6 and the response object is not correctly searialized.

public class AvailableColumns
{
    public List<Column> Columns { get;  } = new List<Column>();
}

public class Column
{
    public string Name { get; set; }
    public List<Value> Values { get; set; } = new List<Value>();
}
public class Value
{       
    public string InternalValue { get; set; }
    public string DisplayName { get; set; }
}

That way the code is read:

// Before this line there are 20 columns in the object T.

response.Data = await response.Content.ReadFromJsonAsync<T>();

// After response.Data has 0 columns

How to fix it?

CodePudding user response:

  1. Not sure how is your api like, be sure your api actually returns AvailableColumns model with data.

  2. Be sure add the set accessor for the Columns property.

     public class AvailableColumns
     {
         public List<Column> Columns { get; set; } = new List<Column>();   //add set accessor here
     }
    
  • Related