我如何访问列表< object>来自AppSettings.json?
在我的appsettings.json中,我有一个类似的字段
"MyFields" : [
{
"name":"one",
"type":"type1"
"parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}]
},
{
"name":"two",
"type":"type2"
...
..
and so on
}
]
我已经制作了一个具有以下属性的Myfield:
public class MyField
{
[JsonProperty]
public string? name { get; set; }
[JsonProperty]
public string? type { get; set; }
[JsonProperty]
public IDictionary<string,string>? parameters { get; set; }
}
我正在尝试使用配置在其他类中访问它
//config is the configuration
var myFields = config.GetSection("MyFields").Get<List<MyField>>();
, 。但是,当我通过消除“参数”字段来做同样的事情时,它就会像魅力一样工作。
我知道这与不当匹配有关,但要获得一些帮助会很棒。
In my appsettings.json I have a field like this
"MyFields" : [
{
"name":"one",
"type":"type1"
"parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}]
},
{
"name":"two",
"type":"type2"
...
..
and so on
}
]
I have made a class Myfield with the following properties:
public class MyField
{
[JsonProperty]
public string? name { get; set; }
[JsonProperty]
public string? type { get; set; }
[JsonProperty]
public IDictionary<string,string>? parameters { get; set; }
}
And I am trying to access it in an other class using the config, like this :
//config is the configuration
var myFields = config.GetSection("MyFields").Get<List<MyField>>();
the problem is that myFields turn out to be empty. But when I do the same thing by eliminating the "parameters" field then it works like a charm.
I know its something related to improper matching but it'll be great to get some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个错误,应该是
,但也许这会更有效
you have a bug, should be
but maybe this would be more effiecient