C#ConfigurationBuilder获取JsonObject
我在使用Configuration Builder访问JSON配置文件的值时遇到问题 我的JSON看起来是这样的C#ConfigurationBuilder获取JsonObject,c#,json,.net-core-2.2,C#,Json,.net Core 2.2,我在使用Configuration Builder访问JSON配置文件的值时遇到问题 我的JSON看起来是这样的 { "item": [ { "valueType": "taktzeit", "interval": 3 }, { "valueType": "werkzeugwechsel", "interval": 5 } ] } 更新 它位于名为Config-->Config/Config.json的文件
{
"item": [
{
"valueType": "taktzeit",
"interval": 3
},
{
"valueType": "werkzeugwechsel",
"interval": 5
}
]
}
更新
它位于名为Config-->Config/Config.json的文件夹中。我设置了属性,因此文件位于build文件夹中
我的代码:
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; }
}
公共类配置
{
公共列表项{get;set;}
}
公共类项目
{
公共字符串ValueType{get;set;}
公共整数间隔{get;set;}
}
假设所有其他配置都已配置,只需绑定到所需的对象图即可
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile(Globals.ConfigPath)
.Build();
Config config = configuration.Get<Config>();
IConfiguration configuration=new ConfigurationBuilder()
.AddJsonFile(Globals.ConfigPath)
.Build();
Config Config=configuration.Get();
参考改变这个
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").GetChildren().ToList().Select(x => x.Value).ToList();
到
var a=builder.AddJsonFile(Globals.ConfigPath.Build())
.GetSection(“项”).Get();
这将显示
项的结果列表
项目中Jason文件的名称是什么?它是appsettings.json文件吗?不是,它是一个文件夹中的文件:Config/Config.json。我已经将文件设置为在生成中。如果我使用…GetSection(“项:0:ValueTyp”)。。我得到文件里的东西。但是我需要一种更通用的方式,我们能看到这个反序列化到的类吗?似乎LINQ中有“x.Value
”,但JSON中有“valueType
”。我更新了我的posttry thisvar a=builder.AddJsonFile(Globals.ConfigPath).Build().GetSection(“item”).Get()代码>查看是否返回列表f项目
var a = builder.AddJsonFile(Globals.ConfigPath).Build()
.GetSection("item").Get<List<Item>>();