-1

I have an appSettings.json file with the following group:

"Devices": {
  "_850347_74Crompton1": "3605",
  "_850532_41Crompton2": "813",
  "_850722_18IMEElectricity": "707",
  "_850766_85DustNoise1": "306",
  "_850772_63Dustnoise2": "2866",
  "_850774_29DustNoise3": "3104",
  "_863859_63Level": "22601",
  "_864233_30": "713",
  "_864319_07noise": "606"
}

My Devices class is:

public class Devices
{
    public string _850347_74Crompton1 { get; set; }
    public string _850532_41Crompton2 { get; set; }
    public string _850722_18IMEElectricity { get; set; }
    public string _850766_85DustNoise1 { get; set; }
    public string _850772_63Dustnoise2 { get; set; }
    public string _850774_29DustNoise3 { get; set; }
    public string _863859_63Level { get; set; }
    public string _864233_30 { get; set; }
    public string _864319_07noise { get; set; }
}

I can easily get to the values in this object, using something like:

var myDevice = config.GetRequiredSection("Devices").Get<Devices>();

And refer to a value of my choice, for example:

var myValue = myDevice._850347_74Crompton1;
var anotherValue = myDevice._864233_30;

But I don't want to hardcode it like this. I want to traverse the list (properties), using something like this:

foreach (Devices d in Devices)
{
   myDevice = d.Key;
   myDeviceValue = d.Value;
}

I just can't see to get it to work. No errors, just getting nulls or not "formed" properly type code.

Thanks.

UPDATE

I've tried this, but I get an error.

Devices iot = config.GetRequiredSection("Devices").Get<Devices>();

foreach (Devices device in iot)
{
   ...
}

Severity Code Description Project File Line Suppression State Error CS1579 foreach statement cannot operate on variables of type 'appSettings.Devices' because 'appSettings.Devices' does not contain a public instance or extension definition for 'GetEnumerator' IoT_CaptisDataCapture .....\Program.cs 60 Active

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
Fandango68
  • 4,461
  • 4
  • 39
  • 74
  • 1
    Do you need all those properties at all? Why not just `class Devices : Dictionary` – Jeremy Lakeman Nov 01 '22 at 04:47
  • You can also use [Options pattern in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0) to get value . – Qing Guo Nov 01 '22 at 09:50

1 Answers1

2

Read the JSON as Dictionary<string, string>.

var myDevice = config.GetRequiredSection("Devices").Get<Dictionary<string, string>>();

foreach (KeyValuePair<string, string> kvp in myDevice)
{
    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • Thanks. That worked. However, can I go a little further? I want to not use KeyValuePair<> but my class definition to the structure - something like appSettings.Devices – Fandango68 Nov 01 '22 at 04:20
  • Can you show your `Devices` class? – Yong Shun Nov 01 '22 at 04:22
  • I've updated OP – Fandango68 Nov 01 '22 at 04:23
  • 1
    A bit not understand your question. *I don't want to hard code the elements of the json list into my program*, so you don't want to add the **properties** of the JSON to your `Device` class. Am I understanding correctly? Or do you mean traverse the properties of `Device` class without coding the property? And FYI, your attached JSON isn't a JSON list/array, but it is an object with multiple keys. – Yong Shun Nov 01 '22 at 04:32
  • Totally re-wrote the OP. Thanks again – Fandango68 Nov 01 '22 at 04:41
  • 1
    More like you need a reflection to iterate the property in a class. [Demo](https://dotnetfiddle.net/CaLstk) – Yong Shun Nov 01 '22 at 04:51