创建多个字典并使用C#追加到列表中 [英] creating multiple dictionary and appending into list using c#

查看:163
本文介绍了创建多个字典并使用C#追加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,正在尝试使用c#创建类似的东西.我们如何做到这一点的任何建议.我只想创建字典列表.

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 = [{服务器":"10.14.13.1",首选":是"},{服务器":"10.1.2.1",首选":否"}]

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

推荐答案

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

注释

  1. 这只是代码片段,我们需要构造函数(a,b)使其完全正常工作.
  2. 您不必自己创建POCO对象,这里有很多在线工具,例如 https://app.quicktype.io/可以帮助您.
  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.

还请参见此链接.

这篇关于创建多个字典并使用C#追加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆