【问题标题】:How can I get json from appsettings.json?如何从 appsettings.json 获取 json?
【发布时间】:2018-05-14 06:42:43
【问题描述】:

如何使用 Microsoft.Extensions.Configuration 获取 appsettings.json 数据? 我的 appsettings.json 文件是:

"Test": [
  {
    "A": "101",
    "B": "6390"
  },
  {
    "A": "101",
    "B": "6391"
  },
  {
    "A": "101",
    "B": "6392"
  }
]

什么时候使用

Configuration.GetSection("Test").GetChildren()

我得到错误:

System.Linq.Enumerable+SelectEnumerableIterator`2[System.String.Microsoft.Extensions.Configuration.IConfigurationSection]

我无法理解如何处理此错误。请询问是否需要任何信息。 这个问题不是重复的,因为我无法正确运行代码,它会停止调试并给出我提到的错误。

哦,实际的错误(如控制台所示)是:

未捕获的 SyntaxError:无效或意外令牌

【问题讨论】:

  • 不适合我
  • 您提供的“错误”不是错误,而是内部迭代器类型名称。请检查并更正。另外,真的请不要将文字作为图片发布。请将文本粘贴为文本,尤其是在这种情况下的重要文本(“这里,看看我得到的错误”......图像是错误的)。这次我努力阅读图像并从图像中手写文字。
  • “不适合我” - “不工作”是一个相当宽泛的术语。它是否有效但不能返回您想要的东西?它根本不起作用吗?有没有错误?在编译时还是在运行时?什么不工作?

标签: c# json visual-studio-code


【解决方案1】:

考虑到您提供的信息很少,也许您只是打错了。

IIRC,appsettings.json 文件需要在根范围内有一个 j 对象。也许 j-array 是可以的。我不记得了。但是你拥有的是一个键值对。对象的切片。大多数 JSON 解析器只会在此类输入上失败。请尝试在 json 文件中的所有文本周围添加括号 {}:

{   //<--- added
  "Test": [
    {
      "A": "101",
      "B": "6390"
    },
    ....
  ]
}   //<--- added

【讨论】:

    【解决方案2】:

    首先要做的是更改您的 json 并添加括号:

    {
      "Test": [
          {
            "A": "101",
            "B": "6390"
          },
          {
            "A": "101",
            "B": "6391"
          },
          {
            "A": "101",
            "B": "6392"
          }
      ]
    }
    

    在您可以使用 IConfiguration 读取您的设置之前,添加以下参考,您可以使用 nuget 数据包管理器来执行此操作:

    您可以执行以下操作:

     // This class can be used for managing your AppSettings file.
    class AppSettings
    {
        private readonly IConfiguration configuration;
    
        public AppSettings(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
    
        // Get the value at an specific key.
        public string GetValue(string key)
        {
            return configuration[key];
        }
    }
    
    class Program
    {
        // This can be used for getting an configuration builder for your config file. Pass in the config name.
        public static IConfigurationBuilder LoadConfiguration(string configFileName)
        {
            // get the file from the current directory and create an builder from it.
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
                .AddJsonFile(configFileName);
    
            return builder;
    
        }
    
        static void Main(string[] args)
        {
            // create configuration builder.
            var configuration = LoadConfiguration("appsettings.json");
    
            // create your AppSettings class and pass the IConfiguration object.
            AppSettings appConfig = new AppSettings(configuration.Build());
    
            // read the value.
            var firstItemAValue = appConfig.GetValue("Test:0:A");
            var secondItemBValue = appConfig.GetValue("Test:1:B");
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 2018-08-02
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多