如何从 appsettings.json 文件中的对象数组中读取值
How to read values from array of objects in appsettings.json file
我的应用设置json文件
{
"StudentBirthdays": [
{ "Anne": "01/11/2000"},
{ "Peter": "29/07/2001"},
{ "Jane": "15/10/2001"},
{ "John": "Not Mentioned"}
]
}
我有一个单独的配置 class。
public string GetConfigValue(string key)
{
var value = _configuration["AppSettings:" + key];
return !string.IsNullOrEmpty(value) ? Convert.ToString(value) : string.Empty;
}
我试过的是,
list= _configHelper.GetConfigValue("StudentBirthdays");
对于以上我没有得到值。
如何读取值(我想分别读取学生的姓名和生日)。
感谢任何帮助
试试这个
using System.Linq;
public List<Student> GetStudentsFromConfig()
{
return _configuration
.GetSection("StudentBirthdays")
.Get<Dictionary<string, string>[]>()
.SelectMany(i => i)
.Select(ie => new Student {Name=ie.Key, DOB=ie.Value})
.ToList();
}
测试
items= _configHelper.GetStudentsFromConfig();
foreach (var item in items) Console.WriteLine($"Name: {item.Name} , DOB: {item.DOB} ");
结果
Name: Anne , DOB: 01/11/2000
Name: Peter , DOB: 29/07/2001
Name: Jane , DOB: 15/10/2001
Name: John , DOB: Not Mentioned
class
public class Student
{
public string Name { get; set; }
public string DOB { get; set; }
}
您可以使用以下代码获取生日:
// 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);
}
试试这个:
像下面这样创建 Model/Class:
public class StudentBirthday
{
String Name,
String Birthday
}
然后像这样访问值:
List<StudentBirthday StudentBirthdays =
_config.GetSection("Main:StudentBirthdays").Get<List<StudentBirthday();
我的应用设置json文件
{
"StudentBirthdays": [
{ "Anne": "01/11/2000"},
{ "Peter": "29/07/2001"},
{ "Jane": "15/10/2001"},
{ "John": "Not Mentioned"}
]
}
我有一个单独的配置 class。
public string GetConfigValue(string key)
{
var value = _configuration["AppSettings:" + key];
return !string.IsNullOrEmpty(value) ? Convert.ToString(value) : string.Empty;
}
我试过的是,
list= _configHelper.GetConfigValue("StudentBirthdays");
对于以上我没有得到值。
如何读取值(我想分别读取学生的姓名和生日)。
感谢任何帮助
试试这个
using System.Linq;
public List<Student> GetStudentsFromConfig()
{
return _configuration
.GetSection("StudentBirthdays")
.Get<Dictionary<string, string>[]>()
.SelectMany(i => i)
.Select(ie => new Student {Name=ie.Key, DOB=ie.Value})
.ToList();
}
测试
items= _configHelper.GetStudentsFromConfig();
foreach (var item in items) Console.WriteLine($"Name: {item.Name} , DOB: {item.DOB} ");
结果
Name: Anne , DOB: 01/11/2000
Name: Peter , DOB: 29/07/2001
Name: Jane , DOB: 15/10/2001
Name: John , DOB: Not Mentioned
class
public class Student
{
public string Name { get; set; }
public string DOB { get; set; }
}
您可以使用以下代码获取生日:
// 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);
}
试试这个: 像下面这样创建 Model/Class:
public class StudentBirthday
{
String Name,
String Birthday
}
然后像这样访问值:
List<StudentBirthday StudentBirthdays =
_config.GetSection("Main:StudentBirthdays").Get<List<StudentBirthday();