如何通过在json设置文件中指定子元素名来获取子元素?

wqsoz72f  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(130)
  1. "AppSettings":
  2. {"AzureAd":
  3. {
  4. "Instance": "https://login.microsoftonline.com/",
  5. "Domain": "abc.com",
  6. "TenantId": "90836560-70ab-4caa-9e10-7e80b43d8d5a"
  7. }
  8. }

如何获取类文件中的“示例”值。我在下面试过了,不起作用。

  1. var symmetrickey = _configuration.GetSection("AppSettings:AzureAd:Instance").GetChildren()
  2. .Select(configSection => configSection.Value);

当我像下面这样修改时,我得到了节点“AzureAd”的数组

  1. var symmetrickey = _configuration.GetSection("AppSettings:AzureAd").GetChildren()
  2. .Select(configSection => configSection.Value);

但我希望通过特别提到文本中的任何一个来获得“Instance”/“TenantId”值。

kqqjbcuj

kqqjbcuj1#

appsetting的JSON不应该以json键开头。删除“AppSettings”:

  1. {
  2. "AzureAd":
  3. {
  4. "Instance": "https://login.microsoftonline.com/",
  5. "Domain": "abc.com",
  6. "TenantId": "90836560-70ab-4caa-9e10-7e80b43d8d5a"
  7. }
  8. }

来获取值

  1. var symmetrickey = _configuration.GetSection("AzureAd:Instance").Value;

相关问题