C# ConfigurationBuilder 获取 JsonObject
C# ConfigurationBuilder Get JsonObject
我在使用配置生成器访问 JSON 配置文件的值时遇到问题
我的 JSON 看起来像那样
{
"item": [
{
"valueType": "taktzeit",
"interval": 3
},
{
"valueType": "werkzeugwechsel",
"interval": 5
}
]
}
更新
它位于名为 Config --> Config/config.json 的文件夹中。我设置了 属性,所以文件在构建文件夹中
我的代码:
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").GetChildren().ToList().Select(x => x.Value).ToList();
这是我循环 "a"'
时得到的结果
看不出我错过了什么。
提前致谢
更新 2
我的模特:
public class Config
{
public List<Item> Item { get; set; }
}
public class Item
{
public string ValueType { get; set; }
public int Interval { get; set; }
}
假设所有其他都已配置,只需绑定到所需的对象图。
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile(Globals.ConfigPath)
.Build();
Config config = configuration.Get<Config>();
引用Configuration in ASP.NET Core
改变这个
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").GetChildren().ToList().Select(x => x.Value).ToList();
到
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").Get<List<Item>>();
这将是 Item
的结果列表
我在使用配置生成器访问 JSON 配置文件的值时遇到问题 我的 JSON 看起来像那样
{
"item": [
{
"valueType": "taktzeit",
"interval": 3
},
{
"valueType": "werkzeugwechsel",
"interval": 5
}
]
}
更新 它位于名为 Config --> Config/config.json 的文件夹中。我设置了 属性,所以文件在构建文件夹中
我的代码:
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").GetChildren().ToList().Select(x => x.Value).ToList();
这是我循环 "a"'
时得到的结果看不出我错过了什么。 提前致谢
更新 2
我的模特:
public class Config
{
public List<Item> Item { get; set; }
}
public class Item
{
public string ValueType { get; set; }
public int Interval { get; set; }
}
假设所有其他都已配置,只需绑定到所需的对象图。
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile(Globals.ConfigPath)
.Build();
Config config = configuration.Get<Config>();
引用Configuration in ASP.NET Core
改变这个
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").GetChildren().ToList().Select(x => x.Value).ToList();
到
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").Get<List<Item>>();
这将是 Item