0
public class providerData
    {
        String rowId { get; set; }
    }

    public class providerPagedData
    {
        public int total { get; set; }
        public int totalPages { get; set; }
        public List<providerData> items { get; set; }
    }
    public class ProviderTest
    {
        String converted = "{\"total\":5,\"totalPages\":0,\"items\":[{\"rowId\":\"#10479\"}]}";
        public ProviderTest()
        {
            providerPagedData providers = new providerPagedData();
            try
            {
                providerPagedData p = JsonSerializer.Deserialize <providerPagedData>(converted,
                        new JsonSerializerOptions
                        {
                            PropertyNameCaseInsensitive = true,
                            
                        }                    
                    );
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

Result returns correct values for members "total" and totalPages, and correct number of "items", but "rowId" of the first element is always null. Can't figure out what I am doing wrong and will appreciate any advise/help

  • 1
    Just a tip: Don't work with JSON strings like that. It becomes a pain to deal with over time. Just create anonymous objects and serialize those. You are guaranteed to have a solid data structure: `var obj = new { total = 5, totalPages = 0, items = new[] { new { rowId = "#10479" } } }` – Andy Aug 18 '20 at 14:07

1 Answers1

2

rowId is not being deserialzied because it is not public. From the docs:

System.Text.Json only works with public properties. Custom converters can provide this functionality.

Changing to this should allow the rowId to deserialize properly:

public class providerData
{
    public String rowId { get; set; }
}
devNull
  • 3,849
  • 1
  • 16
  • 16