Sweetie
Sweetie

Reputation: 1368

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

Upvotes: 1

Views: 4034

Answers (4)

Sweetie
Sweetie

Reputation: 1368

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]

Upvotes: 0

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

Upvotes: 1

Shahzad Khan
Shahzad Khan

Reputation: 530

What is observe, some of fields are missing to make them as nullable like DisplayOrder e.g. 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.

Upvotes: 0

Samaritan_Learner
Samaritan_Learner

Reputation: 557

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.

Upvotes: 1

Related Questions