4

My appsettings json file

       {
         "StudentBirthdays": [
                { "Anne": "01/11/2000"},
                { "Peter": "29/07/2001"},
                { "Jane": "15/10/2001"},
                { "John": "Not Mentioned"}
            ]
        }

I have a seperate config class.

public string GetConfigValue(string key)
{
    var value = _configuration["AppSettings:" + key];
    return !string.IsNullOrEmpty(value) ? Convert.ToString(value) : string.Empty;
}

What I have tried is,

 list= _configHelper.GetConfigValue("StudentBirthdays");

For the above I dont get the values.

How can I read the values(I want to read the name of the student and his birthday seperatly).

Any help is apreciated

6
  • Wha the value of list you get? Commented Sep 24, 2021 at 2:49
  • I'd suggest using .GetSection and iterate through it. Commented Sep 24, 2021 at 2:50
  • @Serge I dont get any values for list.(null) Commented Sep 24, 2021 at 2:51
  • @Llama you mean "_configHelper.GetSection("StudentBirthdays"); " ,after implementing a seperate method to GetSection in config class ? Commented Sep 24, 2021 at 2:59
  • I mean _configuration.GetSection, and yes, probably something like that in the _configHelper class to provide access to it. Commented Sep 24, 2021 at 3:06

3 Answers 3

6

You can obtain the birthdays using the following code:

// get the section that holds the birthdays
var studentBirthdaysSection = _configuration.GetSection("StudentBirthdays");

// iterate through each child object of StudentBirthdays
foreach (var studentBirthdayObject in studentBirthdaysSection.GetChildren())
{
    // your format is a bit weird here where each birthday is a key:value pair,
    // rather than something like { "name": "Anne", "birthday": "01/11/2000" }
    // so we need to get the children and take the first one
    var kv = studentBirthdayObject.GetChildren().First();
    string studentName = kv.Key;
    string studentBirthday = kv.Value;
    Console.WriteLine("{0} - {1}", studentName, studentBirthday);
}

Try it online

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

Comments

1

try this

using System.Linq;

public List<Student> GetStudentsFromConfig()
{
    return  _configuration
    .GetSection("StudentBirthdays")
    .Get<Dictionary<string, string>[]>()
    .SelectMany(i => i)
    .Select(ie => new Student {Name=ie.Key, DOB=ie.Value})
    .ToList();
}

test

items= _configHelper.GetStudentsFromConfig();

foreach (var item in items) Console.WriteLine($"Name: {item.Name} , DOB: {item.DOB} ");

result

Name: Anne , DOB: 01/11/2000 
Name: Peter , DOB: 29/07/2001 
Name: Jane , DOB: 15/10/2001 
Name: John , DOB: Not Mentioned 

class

public class Student
{
    public string Name { get; set; }
    public string DOB { get; set; }
}

2 Comments

I have already implemeted model classes controlleres and everything.And it get value from other keys.As this student birthdays consist an array i find it difficult.Is there a way that i can accomplish this by using same set of file structure that i have created.because what i am trying to do is getting the name and birthday from from db and check whether it is similar to the data in the appsettings or not.
@rissa You should mention it in your question. I updated my answer. You can check again.
-1

Try this: Create Model/Class like below:

public class StudentBirthday
{
   String Name,
   String Birthday
}

Then access values like this :

List<StudentBirthday StudentBirthdays = 
   _config.GetSection("Main:StudentBirthdays").Get<List<StudentBirthday();

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.