How to Read Appsettings.Json With Array of Values

How to read values from array of objects in appsettings.json file

You can obtain the birthdays using the following code:

// get the section that holds the birthdays
var studentBirthdaysSection = _configuration.GetSection("StudentBirthdays");

// iterate through each child object of StudentBirthdays
foreach (var studentBirthdayObject in studentBirthdaysSection.GetChildren())
{
// your format is a bit weird here where each birthday is a key:value pair,
// rather than something like { "name": "Anne", "birthday": "01/11/2000" }
// so we need to get the children and take the first one
var kv = studentBirthdayObject.GetChildren().First();
string studentName = kv.Key;
string studentBirthday = kv.Value;
Console.WriteLine("{0} - {1}", studentName, studentBirthday);
}

Try it online

How to read appsettings.json with array of values

Implement processing of configuration as following somewhere approporiate like this:

var cookieName = 
Configuration.GetSection("SundrySettings:CookieName").Value;
var accessGroup = Configuration.GetSection("SundrySettings:AccessGroup").Value;

var terminals = new List<Terminal>()

var terminalSections = this.Configuration.GetSection("Terminals").GetChildren();
foreach (var item in terminalSections)
{
terminals.Add(new Terminal
{
// perform type mapping here
});
}

SundryOptions sundryOption = new SundryOptions()
{
CookieName = cookieName,
HRAccessGroup = accessGroup,
Terminals = terminalList
};

Of course there could be shorter version, but you can start from here.

How to read sub array of values from appsetting.json using IConfiguration?

try this

PrintAssetDefns[] MyArray = configuration.GetSection("Production:PrintJobs")
.GetChildren().First().GetSection("GroupAssets").Get<PrintAssetDefns[]>();

or you can get all data

PrintJob[] myArray = _config.GetSection("Production:PrintJobs").Get<PrintJob[]>();

classes

public class PrintAssetDefns
{
public string Name { get; set; }
public string SearchPath { get; set; }
public string PrintAssetType { get; set; }
public List<string> ValidExtensions { get; set; }
public List<string> CreativeCodes { get; set; }
}

public class PrintJob
{
public string SalesCategory { get; set; }
public int JobType { get; set; }
public int SubJobType { get; set; }
public int ShipVia { get; set; }
public int CSR { get; set; }
public int SLAHours { get; set; }
public int DaysToArrive { get; set; }
public List<string> Note { get; set; }
public List<PrintAssetDefns> GroupAssets { get; set; }
}

Read json array from appsettings c#

Create an object model to hold the values

public class ProcessStep {
public string name { get; set; }
public bool enabled { get; set; }
}

Then get the array from the section using Get<T>

ProcessStep[] procSteps = _configurationRoot
.GetSection("steps")
.Get<ProcessStep[]>();

ASP.NET Core 1.1 and higher can use Get<T>, which works with entire sections. Get<T> can be more convenient than using Bind

Reference Configuration in ASP.NET Core: Bind to an object graph

Get value from appsettings.json from array of objects

try this

var serverUrl = configuration.GetSection("Serilog").GetSection("WriteTo")
.Get<Dictionary<string,Dictionary<string,string>>[]>()[0]["Args"]["serverUrl"];

or using c# classes

var serverUrl = configuration.GetSection("Serilog").GetSection("WriteTo")
.Get<WriteTo[]>()[0].Args.serverUrl;

classes

public partial class WriteTo
{
public string Name { get; set; }

public Args Args { get; set; }
}

public partial class Args
{
public Uri serverUrl { get; set; }
}


Related Topics



Leave a reply



Submit