简体   繁体   English

从 appsetting.json 中获取字符串值并将其设置为 c# 中的 JSON Key

[英]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做任何假设,但它由字段idfields (均为未知类型)组成,并且当您尝试使用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中的fieldsSomeClass的类型。 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:总结一下:

  • if 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已经是定义良好的类并且您只需要添加对索引的支持或处理一些设置或获取值的自定义逻辑:使用索引器,
  • in any other case - use dictionary.在任何其他情况下 - 使用字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从类中的appsetting.json获取价值 - Get Value from appsetting.json in class 如何使用 c# 获取或设置自定义 appsetting.json - how can I get or set custom appsetting.json with c# Appsetting.json在Controller类中读取键的值,是否为空? - Appsetting.json read value of key in Controller Class,Comes Blank? 如何从 appsetting.json 读取 json 数据 - How to read json data from appsetting.json 来自 appsetting.json (.NET Core) 的 WCF 服务客户端配置 - WCF service client configuration from appsetting.json (.NET Core) 如何从任何 class blazor 服务器端读取 appsetting.json - How to read appsetting.json from any class blazor serverside 如何动态获取 appsetting.json 值 - How can I get the appsetting.json value dynamically Configuration.GetSection 从 appsetting.json 获取值但 Configuration.GetSection.Bind 总是返回 null - Configuration.GetSection gets value from appsetting.json but Configuration.GetSection.Bind always returns null 如何从 appsetting.json 获取 ConnectionString 值到核心 WebAPI 的存储库中 - How to get ConnectionString Value from appsetting.json into Repository in Core WebAPI 从vNext中的appsetting.json读取启动外的连接字符串 - read connectionstring outside startup from appsetting.json in vNext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM