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?

5
  • How about a List<KeyValuePair<string,string>> insetad of List<Ip> in `Cfg' class? Commented Nov 9, 2016 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? Commented Nov 9, 2016 at 13:43
  • Your desired JSON is invalid. Upload to 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. Commented Nov 9, 2016 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! Commented Nov 9, 2016 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 - dotnetfiddle.net/PaBSQc Commented Nov 9, 2016 at 14:04

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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
Perfect, fully understood your code and adpated it! Thanks!
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.

Comments

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.