问题
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:
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
- this is code snippet only, and we need constructor(a,b) to make it fully working.
- 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.
来源:https://stackoverflow.com/questions/61794865/creating-multiple-dictionary-and-appending-into-list-using-c-sharp