We have a class as shown below:
public class KeyVal<Key, Val>
{
public Key Id { get; set; }
public Val Text { get; set; }
public KeyVal() { }
public KeyVal(Key key, Val val)
{
this.Id = key;
this.Text = val;
}
}
We are sending a Json string from the client using javascript to our controller action as follows:
[{"id":7.4,"text":false},{"id":9.0,"text":true}]
which when looked at the controller as an argument it looks perfectly ok and identical to what was sent:
[{"id":7.4,"text":false},{"id":9.0,"text":true}]
We are trying to deserialize this into the calss above using the following code:
var deserialized= System.Text.Json.JsonSerializer.Deserialize<List<KeyVal<float?, bool>>>(list)
But ufortunately we keep getting nulls for the id and always false for the text property. i.e. its not being deserialized into an instance of our class.
Any ideas?
public class KeyVal<Key, Val> { [JsonPropertyName("id")] public Key Id { get; set; } [JsonPropertyName("text")] public Val Text { get; set; } public KeyVal() {} public KeyVal(Key key, Val val) { Id = key; Text = val; } }JsonSerializerOptions.PropertyNameCaseInsensitive = true, or setJsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase, both as shown in JsonSerializer.Deserialize fails.JsonSerializerOptions.Webwhich sets both PropertyNameCaseInsensitive and CamelCase, see dotnetfiddle.net/Zdm7tW. So should we mark your question as a duplicate of the other question?