Today when I was trying to convert a JSON string with System.Text.Json I was finding that it was creating objects for all the elements in my JSON string but all the properties were blank.
I figured that it must be something to do with the way that is was converting the casing of the JSON attributes to C# properties.
The failing code can be seen below. It works without throwing errors but the resultant IEnumerable<WeatherForcast> contains an array of WeatherForcast objects that have null properties.
HttpClient httpClient = new HttpClient();var response = await httpClient.GetAsync("http://localhost:5000/api/weatherforecast");var str = response.Content.ReadAsStringAsync().Result;var obj = JsonSerializer.Deserialize<IEnumerable<WeatherForecast>>(str);return obj;
As a quick fix I simply asked the JsonSerializer to ignore case, by passing a JsonSerializerOptions object as the second parameter to the Deserialize function.
In my case I set the options.PropertyNameCaseInsensitive = true; but I could have equally set the correct JsonNamingPolicy.
var options = new JsonSerializerOptions();options.PropertyNameCaseInsensitive = true;var obj = JsonSerializer.Deserialize<IEnumerable<WeatherForecast>>(str,options);