Home > Blockchain >  How can I read particular data structure from appSettings.json?
How can I read particular data structure from appSettings.json?

Time:11-30

I've the follow data structure in my appSettings.json.

"Ranges": [
  {
    "K": 1,
    "Min": 2,
    "Max": 3
  },
  {
    "K": 2,
    "Min": 4,
    "Max": 6
  }
]

What is data structure should I use in my C# code for reading that? I know that I can't use cortage, in this case I would be use the follow code:

class RangeOptions
{
   public List<(int K, int Min, int Max)> Ranges {get;set;} //What should I use into List<?>
}   

But as I know, it doesn't fit for reading this structure. Could you help me with my issue please?

Thank you, guys!

CodePudding user response:

var section = configuration.GetSection("Ranges");
var Ranges= section.Get<List<Range>>();

Class

Class Public Range
{
 public int K {get;set;}
 public int Min {get;set;}
 public int Max {get;set;}
}

Or By Linq

var desire = configuration
   .GetSection("Ranges")
   .GetChildren()
   .Select(x => x.yourvalue);
  • Related