[英]How to read complex objects from json file in .net core
I have a json like this to read in c# but it showing null. 我有一个这样的json可以在c#中读取,但显示为null。
Provices.json contains this json Provices.json包含此json
{
"Provinces": {
"States": [
{
"CountryId": 1,
"Name": "AA (Armed Forces Americas)",
"Abbreviation": null,
"Published": false,
"DisplayOrder": 0,
"Country": null,
"Id": 1
},
{
"CountryId": 1,
"Name": "AE (Armed Forces Europe)",
"Abbreviation": null,
"Published": false,
"DisplayOrder": 0,
"Country": null,
"Id": 54
}
]
}
}
start up file contains 启动文件包含
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.Configure<Provinces>(Configuration.GetSection("Provinces"));
This is c# models 这是C#模型
public class Provinces
{
public Provinces()
{
this.States = new List<State>();
}
public List<State> States { get; set; }
}
public class State
{
public int CountryId { get; set; }
public string Name { get; set; }
public string Abbreviation { get; set; }
public bool Published { get; set; }
public bool DisplayOrder { get; set; }
public string Country { get; set; }
public int Id { get; set; }
}
This is how I am reading this file which is giving me null values 这就是我读取此文件的方式,该文件为我提供了空值
public class UserService
{
public UserService(IOptions<AppSettings> appSettings,
IOptions<Provinces> states)
{
_appSettings = appSettings;
_states = states;
}
//Getting value from json
public List<State> GetStates()
{
var data = this._states.Value.States; // Zero count shows here
return new List<State>();
}
}
I am also reading AppSettings.json which is working fine but province.json is not working.Can You please tell what's wrong i did 我也正在阅读AppSettings.json,它可以正常工作,但province.json无法正常工作。请问我做错了什么
You have both Published
and DisplayOrder
declared as bool
but in the file, the values are: 您已将Published
和DisplayOrder
声明为bool
但在文件中,值是:
"Published": false,
"DisplayOrder": 0,
Also, when you read your values into data
in the code below, you return a new empty list. 同样,当您在下面的代码中将值读入data
时,将返回一个新的空列表。 You need to return data
instead. 您需要返回data
。
public List<State> GetStates()
{
var data = this._states.Value.States; // Zero count shows here
return new List<State>();
}
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJson(yourconfigSetting.json optional: true, reloadOnChange: true)
.AddJsonFile("Provinces.json", optional: true, reloadOnChange: true)
.Build();
If you call as separate file like this then it will come, or else you can add the contents of Provinces to your appconfig json file and get it as section for that your current code will work. 如果您这样调用作为单独的文件,则它将来,否则您可以将Provinces的内容添加到您的appconfig json文件中,并将其作为该节的一部分,以使您的当前代码可用。
I got my problem. 我有我的问题。
Well, the answer was in type. 好吧,答案是正确的。
"CountryId": 1,
"Name": "AE (Armed Forces Europe)",
// "Abbreviation": null,
// "Published": false,
//"DisplayOrder": 0,
// "Country": null,
"Id": 54
On commenting out these lines, it started working. 在注释掉这些行后,它开始工作。
Second Mistake is to add json file in Program.cs[.net core version 2.0] 第二个错误是在Program.cs [.net核心版本2.0]中添加json文件
What is observe, some of fields are missing to make them as nullable like DisplayOrder eg json is not loading because of null property. 可以看到,缺少某些字段使它们像DisplayOrder一样可为空,例如,由于null属性而未加载json。
public class State
{
public int CountryId { get; set; }
public string Name { get; set; }
public string Abbreviation { get; set; }
public bool Published { get; set; }
public bool DisplayOrder { get; set; }
public string Country { get; set; }
public int Id { get; set; }
}
Note: Expect string property no need make them as null, rest type of parameter we need to make him as null and if you sure property also contain value then no need made him as a null parameter. 注意:期望字符串属性不需要将其设置为null,其余类型的参数需要将其设置为null,如果您确定属性也包含值,则无需将其设置为null参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.