C# .NetCore-从JSON文件获取数组
我需要从asp.net核心中的configuration.json文件中获取数组。 我创建了一个API,可以从配置文件中获取一个节,但什么也没有得到。 请帮我弄清楚! 这是configuration.json文件C# .NetCore-从JSON文件获取数组,c#,asp.net-core,.net-core,configuration,json.net,C#,Asp.net Core,.net Core,Configuration,Json.net,我需要从asp.net核心中的configuration.json文件中获取数组。 我创建了一个API,可以从配置文件中获取一个节,但什么也没有得到。 请帮我弄清楚! 这是configuration.json文件 "Settings": { "Standards": [ "LKG", "UKG", "I", "II", "III", "IV
"Settings": {
"Standards": [ "LKG", "UKG", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "COL" ],
"PaymentModes": [ "CASH", "DD", "TRANSFER", "RTGS", "NEFT", "CC", "NB" ],
"FeeTypes": [ "SCHOOL FEE", "HOSTEL FEE", "TRANSPORTATION FEE", "WELCOME PACK FEE", "TOUR FEE", "COACHING FEE", "OTHER FEE" ],
"Installments": [ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" ],
"FinancialInstitutes": ["FEDERAL","ICICI","PAYU","AXIS","AXISRTGS","PAYTM"]
}
这是api
[HttpGet]
[AllowAnonymous]
[Route("Settings")]
public List<string> GetSettings()
{
var settings = Configuration.GetSection("Settings").Get<List<string>>();
return settings;
}
[HttpGet]
[异名]
[路线(“设置”)]
公共列表GetSettings()
{
var settings=Configuration.GetSection(“设置”).Get();
返回设置;
}
这是启动课程
public class Startup
{
private readonly string _contentRootPath;
public IWebHostEnvironment env;
public IConfiguration Configuration { get; }
public Startup(IWebHostEnvironment env)
{
_contentRootPath = env.ContentRootPath;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("configuration.json", optional: true, reloadOnChange: true)
.AddJsonFile($"configuration.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
this.env = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfigurationRoot>(provider => { return (IConfigurationRoot)Configuration; }); services.AddMvc()
.AddControllersAsServices();
services.AddHttpContextAccessor();
}
}
公共类启动
{
私有只读字符串\u contentRootPath;
公共IWebHostEnvironment环境;
公共IConfiguration配置{get;}
公共启动(IWebHostEnvironment env)
{
_contentRootPath=env.contentRootPath;
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“configuration.json”,可选:true,重载更改:true)
.AddJsonFile($“配置{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
this.Configuration=builder.Build();
this.env=env;
}
public void配置服务(IServiceCollection服务)
{
services.AddSingleton(provider=>{return(IConfigurationRoot)Configuration;});services.AddMvc()
.addControllerAsservices();
AddHttpContextAccessor();
}
}
首先,尝试创建一个类作为“设置”部分,如:
public class Settings
{
public List<string> Standards {get; set;}
public List<string> PaymentModes {get; set;}
public List<string> FeeTypes {get; set;}
public List<string> Installments {get; set;}
public List<string> FinancialInstitutes {get; set;}
}
我在startup类中为配置提供了单例依赖项,这很有效。 这就是问题所在。我只是修改了我的代码,首先获取所有键(数组),然后在foreach循环中,我通过它逐个获取的键来获取数组的值
public Dictionary<string, List<string>> GetSettings()
{
var response = new Dictionary<string, List<string>>();
var settings = Configuration.GetSection("Settings").GetChildren().Select(x => x.Key).ToList();
foreach (var setting in settings)
{
response.Add(setting, Configuration.GetSection($"Settings:{setting}").Get<List<string>>());
}
return response;
}
公共字典GetSettings()
{
var response=newdictionary();
var settings=Configuration.GetSection(“settings”).GetChildren().Select(x=>x.Key).ToList();
foreach(设置中的变量设置)
{
Add(setting,Configuration.GetSection($“Settings:{setting}”).Get();
}
返回响应;
}
您正在尝试获取一维数组。但在json配置中,您有二维数组。尝试将列表替换为字符串[][],或者尝试将列表替换为响应中的null。{“Standards”:null,“PaymentModes”:null,“FeeTypes”:null,“Installments”:null,“FinancialInstitutes”:null}您是否将IConfiguration注入构造函数?如果是,请尝试将设置类中的
列表更改为字符串[]
public Dictionary<string, List<string>> GetSettings()
{
var response = new Dictionary<string, List<string>>();
var settings = Configuration.GetSection("Settings").GetChildren().Select(x => x.Key).ToList();
foreach (var setting in settings)
{
response.Add(setting, Configuration.GetSection($"Settings:{setting}").Get<List<string>>());
}
return response;
}