[英]Take string value from appsetting.json and set it to JSON Key in c#
I am taking the key from appsetting.json into c# code.我将 appsetting.json 中的密钥转换为 c# 代码。 Now I have to capture that and get the value from JSON Response in foreach loop c#
现在我必须捕获它并在 foreach 循环 c# 中从 JSON 响应中获取值
In appsetting.Json: "CustomField": "customfield_10118"
在 appsetting.Json:
"CustomField": "customfield_10118"
and code where i have to capture the value:和我必须捕获值的代码:
Maybe You are in need of indexer ?也许您需要索引器?
As I cannot assume anything about k
but it consists of fields id
and fields
(both unknown type) and as You are trying to use a string
variable as a key You would either need the fields
to be of type IDictionary<string, ...>
or You can provide an indexer for the class of fields
.因为我不能对
k
做任何假设,但它由字段id
和fields
(均为未知类型)组成,并且当您尝试使用string
变量作为键时,您要么需要这些fields
的类型为IDictionary<string, ...>
或者您可以为fields
类提供索引器。
1) Dictionary: 1) 字典:
k.fields = new Dictionary<string, string>();
...
string CustomField = ...;
epicKey = k.fields[CustomField]
2) Indexer 2) 索引器
Let assume that fields
inside k
is a type of SomeClass
.假设
k
中的fields
是SomeClass
的类型。 Then inside SomeClass
:然后在
SomeClass
里面:
public class SomeClass
{
...
public string this[string key] {
get {
return ...
}
set {
...
}
}
}
assuming that You are storing all those values somewhere else and indexer is only a way to get them from it, to make this syntax proper:假设您将所有这些值存储在其他地方,而索引器只是从中获取它们的一种方法,以使此语法正确:
string CustomField = ...;
epicKey = k.fields[CustomField]
To summarise:总结一下:
fields
is already well defined class and You just need to add a support of indexing or handle some custom logic of setting or getting values: use indexer,fields
已经是定义良好的类并且您只需要添加对索引的支持或处理一些设置或获取值的自定义逻辑:使用索引器,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.