creating multiple dictionary and appending into list using c#

▼魔方 西西 提交于 2020-06-13 12:25:49

问题


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

  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.



来源:https://stackoverflow.com/questions/61794865/creating-multiple-dictionary-and-appending-into-list-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!