如何从.net核心中的json文件读取复杂对象

 收藏

我有一个这样的json可以在c#中读取,但显示为null。

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
          }
    ]
  }
}

启动文件包含

 services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
 services.Configure<Provinces>(Configuration.GetSection("Provinces"));

这是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; }

    }

这就是我读取此文件的方式,该文件为我提供了空值

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>(); 
        }

}

我也正在阅读AppSettings.json,它可以正常工作,但province.json无法正常工作。请问我做错了什么

回复
  • 32场 回复

    var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJson(yourconfigSetting.json optional: true, reloadOnChange: true)
    .AddJsonFile("Provinces.json", optional: true, reloadOnChange: true)
    .Build();
    

    如果您这样调用作为单独的文件,则它将来,否则您可以将Provinces的内容添加到您的appconfig json文件中,并将其作为该节的一部分,以使您的当前代码可用。

  • 大四喜 回复

    观察到,缺少某些字段以使它们像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; }
    
        }
    

    注意:期望字符串属性不需要将其设置为null,其余类型的参数需要将其设置为null,如果您确定属性也包含值,则无需将其设置为null参数。

  • Stanf 回复

    我有我的问题。

    好吧,答案是正确的。

            "CountryId": 1,
            "Name": "AE (Armed Forces Europe)",
           // "Abbreviation": null,
           // "Published": false,
           //"DisplayOrder": 0,
           // "Country": null,
            "Id": 54
    

    在注释掉这些行后,它开始工作。

    第二个错误是在Program.cs [.net核心版本2.0]中添加json文件

  • 打酱油 回复

    You have both Published and DisplayOrder declared as bool but in the file, the values are:

    "Published": false,
    "DisplayOrder": 0,
    

    Also, when you read your values into data in the code below, you return a new empty list. You need to return data instead.

    public List<State> GetStates()
        {
            var data = this._states.Value.States; // Zero count shows here
            return new List<State>(); 
        }