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 usingBind
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
Reading Large Text Files With Streams in C#
How to Get the Specific Column in Excel Worksheet Using Documentformat.Openxml C#
How to Delete All Files in an Azure File Storage Folder
Configurationmanager.Appsettings Getting Null
How to Write Data on Multiple Lines But Within the Same Cell of Csv
How to Get the Currently Loggedin Windows Account from an ASP.NET Page
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
How to Add Json to Restsharp Post Request
How to Cast a List into a Type Which Inherits from List<T>
Get and Post Methods With the Same Action Name in the Same Controller
Linq - Select Date from Datetime
How to Check, If Bitmask Contains Bit
Take String Before and After 'First' Space Character
How to Determine Browser Window Size on Server Side C#
Add Header and Footer for Pdf Using Itextsharp