I'm trying to bind a JSON file to a model for configuration settings in a dotnet core project.
The json looks like:
"Settings": {
"Values": [
{ "Value1": "1" },
{ "Value2": "2" }
]}
My model looks like:
public class Settings
{
public List<Value> Values{ get; set; }
}
public class Value
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
I bind with the following:
var settings = _configuration.GetSection("Settings").Get<Settings>();
But this results in settings containing two Value objects in the list - the first Value object has Value1 = 1 & Value2 = null, and the second Value object has Value1 = null & Value2 = 2. How can I bind such that it settings only has one Value object in Values with both properties populated?
This is not the same as suggested here: https://stackoverflow.com/questions/51488845/how-to-read-a-string-array-in-appsettings-json
This is because each object in the array in this case have different properties whereas in the proposed duplicate all objects are consistent.
"Values": [ { "Value1": "1", "Value2": "2" } ]