Home > Blockchain >  Can't deserialize System.Collections.ObjectModel.Collection in a complex object
Can't deserialize System.Collections.ObjectModel.Collection in a complex object

Time:10-29

I have this complex object which include multiple property of "System.Collections.ObjectModel.Collection", but this collection won't deserialized, it always return with nothing in the element. for example:

public class ProductCategory
{
    public string ProductCategoryName;
    public System.Collections.ObjectModel.Collection<Product> Product
}

{
  "ProductCategoryName": "Appliance",
  "Product": [
    {
       "entry1": "entry1",
       "entry2": "entry2"
    },
    {
       "entry1": "entry1",
       "entry2": "entry2"
    }
  ]
}


var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        };

var result = JsonSerializer.Deserialize<ProductCategory>(productCategoryString, options);

In the result, Product has 0 items, ProductCategoryName deserialized properly. any help would be appreciated.

CodePudding user response:

I made a fiddle and it worked. I suspect that "Product" lacks public setters or setters at all. have a look at this fiddle: https://dotnetfiddle.net/GcGoUK

  • Related