简体   繁体   中英

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.

Provices.json contains this 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

   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

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

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]

What is observe, some of fields are missing to make them as nullable like DisplayOrder eg json is not loading because of null property.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM