creating multiple dictionary and appending into list using c#

418 Views Asked by At

I am new to C# and am trying to create something like this using c#. How we can do that any suggestion. I just want to create list of dict.

a = [ { "server": "10.14.13.1", "prefer": "Yes" }, { "server": "10.1.2.1", "prefer": "No" } ]

1

There are 1 best solutions below

6
David On
public class POCO
{
    [JsonProperty("server")]
    public string Server { get; set; }

    [JsonProperty("prefer")]
    public string Prefer { get; set; }
}

POCO o1 = new POCO("10.1.2.1", "No");
POCO o2 = new POCO("10.1.2.2", "YES");
List<POCO> list = new List<POCO>() {o1, o2};

JsonConvert.SerializeObject(list);  //<-- this is what you want

Notes

  1. this is code snippet only, and we need constructor(a,b) to make it fully working.
  2. You don't have to create the POCO object yourself, there is plenty of online tools, e.g. https://app.quicktype.io/ to help you in this.

Refer to this link also.

enter image description here