0

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?

Nadeem Khoury
  • 886
  • 6
  • 18
PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • How about a `List>` insetad of `List` in `Cfg' class? – Developer Nov 09 '16 at 13:37
  • What is the end goal? You want to check the Volume per IP specified and write code for that or correct the format of jSON? – A3006 Nov 09 '16 at 13:43
  • Your desired JSON is invalid. Upload to http://jsonlint.com/ and you will see the error `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](https://stackoverflow.com/questions/38793151/deserialize-nested-json-into-c-sharp-objects). – dbc Nov 09 '16 at 13:46
  • I was not sure if the format is correct but that's not the issue, I think the link from dbc should be sufficient, so thanks for now! – PrimuS Nov 09 '16 at 13:51
  • In case of keyValuePair, the sample output will be (for list of 2 Sonos with 5 ip's each) - `[{"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 - https://dotnetfiddle.net/PaBSQc – Developer Nov 09 '16 at 14:04

2 Answers2

1

You JSON is invalid, I assume your JSON is:

{
  "Sonos": [
    {
      "192.168.10.214": [
        {
          "Volume": "5"
        }
      ],
      "192.168.10.204": [
        {
          "Volume": "5"
        }
      ]
    }
  ]
}

If so, you could deserialize it via this class structure:

class Container
{
    public List<Dictionary<string, List<Cfg>>> Sonos { get; set;}
}

class Cfg
{
    public string Volume { get; set; }
}

// ...
var container = JsonConvert.DeserializeObject<Container>(json);

Creating a model for the JSON could be achieved via:

var container = new Container();
container.Sonos = new List<Dictionary<string, List<Cfg>>>
{
    new Dictionary<string, List<Cfg>>
    {
        { "192.168.10.214", new List<Cfg> { new Cfg { Volume = "5" } } }
    }
};

var json = JsonConvert.SerializeObject(container);
Nico
  • 3,542
  • 24
  • 29
  • I think the question is not clear enough, but I want to CREATE the json (as in your code), with this code, how would I add/create values to the json – PrimuS Nov 09 '16 at 13:58
  • 1
    Perfect, fully understood your code and adpated it! Thanks! – PrimuS Nov 09 '16 at 14:17
0

if your working with VS 2015 and you already have an existing Json, or Xml, structure and only want to have the matching type for deserializing, there is a feature called "paste Special" in edit menu. You only need to have the json or xml in clipboard and vs2015 will generate the classes for you.

Dom84
  • 852
  • 7
  • 20