1

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

4
  • 1
    you meant to say that here no values are coming right "_states = states"? Commented May 30, 2019 at 6:52
  • yes. no values in states. The count is zero Commented May 30, 2019 at 6:53
  • You are taking the Provinces as section in your startup, try to add it as separate file like this " .AddJsonFile("Provinces.json") Commented May 30, 2019 at 7:01
  • Instead of doing it as separate file you can place them in config file only then take the section from configuration like above your code then it will work. Commented May 30, 2019 at 7:04

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

1 Comment

No. Its not working. Moreover If I add this array in appsettings, then also this does not work. There is something else which is wrong
1

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

Comments

0

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.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.