ASP.NET Core 读取配置文件JSON 数据、数组

配置访问接口

public  IConfiguration _Config;
public 类名 (IConfiguration Config)
{
  _Config = Config;
}

配置文件数据示例

{
    "AllowedHosts": "*",
    "Users": [
      {
        "Id": "123",
        "Username": "me"
      },
      {
        "Id": "456",
        "Username": "notme"
      }
    ]
  }
var allowedHosts=_Config["AllowedHosts"];  // * 
var allowedHosts=_Config["Users:1:Id"];  // 123
var allowedHosts=_Config["Users:2:Id"];  // 456


//获取整个数组的所有值
var UserData = _Config.GetSection("Users");
var UserArray = UserData.AsEnumerable();

 基于Json数组创建Users模型

public class Users
{
    public string Id { get; set; }
    public string Username { get; set; }
}
List<Users> TestUsers = _Config.GetSection("Users").Get<List<Users>>();

 

posted @ 2022-09-19 22:52  林小强  阅读(112)  评论(0编辑  收藏  举报