【问题标题】:How to read values from array of objects in appsettings.json file如何从 appsettings.json 文件中的对象数组中读取值
【发布时间】:2021-11-17 08:49:11
【问题描述】:

我的 appsettings json 文件

       {
         "StudentBirthdays": [
                { "Anne": "01/11/2000"},
                { "Peter": "29/07/2001"},
                { "Jane": "15/10/2001"},
                { "John": "Not Mentioned"}
            ]
        }

我有一个单独的配置类。

public string GetConfigValue(string key)
{
    var value = _configuration["AppSettings:" + key];
    return !string.IsNullOrEmpty(value) ? Convert.ToString(value) : string.Empty;
}

我尝试过的是,

 list= _configHelper.GetConfigValue("StudentBirthdays");

对于上述我没有得到值。

我如何读取这些值(我想分别读取学生的姓名和他的生日)。

感谢任何帮助

【问题讨论】:

  • 你得到的list的价值是多少?
  • 我建议使用.GetSection 并遍历它。
  • @Serge 我没有得到任何列表值。(null)
  • @Llama 你的意思是 "_configHelper.GetSection("StudentBirthdays"); ",在配置类中实现一个单独的 GetSection 方法之后?
  • 我的意思是_configuration.GetSection,是的,可能是在_configHelper 类中提供对它的访问。

标签: c# arrays object configuration appsettings


【解决方案1】:

您可以使用以下代码获取生日:

// 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

【讨论】:

  • 非常感谢
【解决方案2】:

试试这个

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 

public class Student
{
    public string Name { get; set; }
    public string DOB { get; set; }
}

【讨论】:

  • 我已经实现了模型类控制器和所有东西。它从其他键中获得价值。由于这个学生的生日包含一个数组,我发现它很困难。有没有一种方法可以通过使用相同的方法来完成我创建的一组文件结构。因为我想做的是从 db 中获取名称和生日,并检查它是否与 appsettings 中的数据相似。
  • @rissa 您应该在问题中提及它。我更新了我的答案。你可以再检查一下。
【解决方案3】:

试试这个: 创建模型/类如下:

public class StudentBirthday
{
   String Name,
   String Birthday
}

然后像这样访问值:

List<StudentBirthday StudentBirthdays = 
   _config.GetSection("Main:StudentBirthdays").Get<List<StudentBirthday();

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
相关资源
最近更新 更多