Not sure if the title matches what I actually want, suggestions welcome.
I create a JSON file like this:
{
"Sonos": [
{
"Ips": [
{
"Volume": "5",
"ip": "192.168.10.214"
},
{
"Volume": "5",
"ip": "192.168.10.204"
}
]
}
]
}
Class
public class GetConfig
{
public List<Cfg> Sonos { get; set; }
}
public class Cfg
{
public List<Ip> Ips { get; set; }
}
public class Ip
{
public string Volume { get; set; }
public string ip { get; set; }
}
Create JSON
var list = new List<Cfg>();
var ips = new List<Ip>();
foreach (ListViewItem item in sonosListExt1.Items)
{
ips.Add(new Ip {Volume = "5", ip = item.Text });
}
list.Add(new Cfg { Ips = ips });
var gC = new GetConfig
{
Sonos = list
};
...
//WRITE TO FILE
What I actually want (not sure if valid though)
{
"Sonos": [
{
"192.168.10.214": [
{
"Volume": "5"
},
"192.168.10.204": [
{
"Volume": "5"
}
]
}
]}
I don't know how I can create the actual IP as an object that contains the Volume of the said IP. Or maybe I need a different approach?
What I want to do
I have a list with IPs, loop through this list and want to get the Volume from the config.json file, is there maybe a better way?
List<KeyValuePair<string,string>>insetad ofList<Ip>in `Cfg' class?Error: Parse error on line 6:. In general, if you want the IP address to map to the JSON property name, you should use a dictionary with the IP address as the key. See e.g. Deserialize nested JSON into C# objects.[{"Ips":[{"Key":"10.1.2.0","Value":"0"},{"Key":"10.1.2.1","Value":"1"},{"Key":"10.1.2.2","Value":"2"},{"Key":"10.1.2.3","Value":"3"},{"Key":"10.1.2.4","Value":"4"}]},{"Ips":[{"Key":"10.1.2.0","Value":"0"},{"Key":"10.1.2.1","Value":"1"},{"Key":"10.1.2.2","Value":"2"},{"Key":"10.1.2.3","Value":"3"},{"Key":"10.1.2.4","Value":"4"}]},{"Ips":[{"Key":"10.1.2.0","Value":"0"},{"Key":"10.1.2.1","Value":"1"}.....]Fiddle here - dotnetfiddle.net/PaBSQc