【问题标题】:List<IDictionary<string, object>> adding more List<IDictionary<string, object>> with titleList<IDictionary<string, object>> 添加更多 List<IDictionary<string, object>> 和标题
【发布时间】:2017-11-27 09:28:07
【问题描述】:

嘿,我正在尝试添加(或合并,如果你想这样称呼它)我创建的几个 List&lt;IDictionary&lt;string, object&gt;&gt;,看起来像这样:

List<IDictionary<string, object>> eachTblColumnProperties = new List<IDictionary<string, object>>
{
   new Dictionary<string, object>
   {{ "title", "Type" },{"name", "Type" },{ "type", "text" },{ "width", "80" },{ "align", "left" },
   { "filtering", "true" },{ "visible", "true" },{ "sorting", "true" }},
   new Dictionary<string, object>
   {{ "title", "Description" },{"name", "Description" },{ "type", "text" },{ "width", "80" },{ "align", "left" },
   { "filtering", "true" },{ "visible", "true" },{ "sorting", "true" }},
   etc etc...........
};

我将有大约 6 个 List&lt; IDictionary&lt; string, object&gt;&gt; 我希望将其转换为 JSON(使用 json.net)。

所以它们的 JSON 输出如下所示:

目前我只返回 1 个名为 List&lt; IDictionary&lt; string, object&gt;&gt;status

return JsonConvert.SerializeObject(
    new { status = eachTblColumnProperties }, 
    Formatting.Indented);

看起来像:

{ 
  "status" [{
     "title":"Type",
     "name":"Type",
     "type":"text",
     "width":"80",
     "align":"left",
     "filtering":"true",
     "visible":"true",
     "sorting":"true"
 },{
     "title":"Description",
     "name":"Description",
     "type":"text",
     "width":"80",
     "align":"left",
     "filtering":"true",
     "visible":"true",
     "sorting":"true"
 },{
   ... etc etc...
 }]
}

这很好用,但我需要能够在一个回电中发送我所有的List&lt; IDictionary&lt; string, object&gt;&gt;...

所以看起来像这样:

 {
    "status": [{
            "title": "Type",
            "name": "Type",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },
        {
            "title": "Description",
            "name": "Description",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },{
          etc..etc...
        }
    ],
    "AnotherStatus": [{
            "title": "Type",
            "name": "Type",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },
        {
            "title": "Description",
            "name": "Description",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        }, {
          etc... etc...
        }
    ]
 }

【问题讨论】:

  • 你有什么问题?
  • 我认为您正在尝试将所有字典名称作为它们所保存数据的键。如果是这种情况,只需另一个字典将名称存储为键,值是与其关联的字典
  • 听起来Dictionary&lt;string, List&lt;Dictionary&lt;string, string&gt;&gt;&gt; 更符合您的要求。

标签: c# json list json.net idictionary


【解决方案1】:

你想要的是Dictionary&lt;string, List&lt;Dictionary&lt;string, object&gt;&gt;&gt;

例子:

Dictionary<string, List<Dictionary<string, object>>> eachTblColumnProperties = new Dictionary<string, List<Dictionary<string, object>>>
{
    { "Status", new List<Dictionary<string, object>> 
                {
                    new Dictionary<string, object>
                    {
                        { "title", "Type" },
                        {"name", "Type" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    },
                    new Dictionary<string, object>
                    {
                        { "title", "Description" },
                        {"name", "Description" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    }
                }},
    { "AnotherStatus", new List<Dictionary<string, object>> 
                {
                    new Dictionary<string, object>
                    {
                        { "title", "Type" },
                        {"name", "Type" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    },
                    new Dictionary<string, object>
                    {
                        { "title", "Description" },
                        {"name", "Description" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    }
                }}
};

这应该会生成您想要的 JSON。

EDIT 忘记将new 放在列表之前。工作小提琴here

【讨论】:

  • 这也不错,Maccettura。谢谢!
  • 嗯...我得到了很多红色曲线的代码。图片见 OP。
  • @StealthRT 修复了它并添加了一个小提琴
  • 谢谢 maccettura。效果很好。
【解决方案2】:

你要这样的东西吗?

public class MyReturnType
{
    public List<IDictionary<string, object>> Status { get; set; }
    public List<IDictionary<string, object>> SomethingElse { get; set; }
}

var myReturnType = new MyReturnType
{
    Status = statusList,
    SomethingElse = someOtherList
};

return JsonConvert.SerializeObject(myReturnType, Formatting.Indented);

【讨论】:

  • 不错的卡米洛!感谢那。这看起来正是我所需要的。
  • @StealthRT 我投了反对票,因为您在示例中使用了匿名类,这个答案使一个具体的类全部用于序列化。它的矫枉过正和限制。
  • @maccettura OP 声明他们希望它是动态的吗?不。 OP 是否说他不想要具体类型?不。这也可以在没有类的情况下使用匿名类型来完成,但是在不同的方法中记住结构会很糟糕。
  • @CamiloTerevinto OP 显然使用了一个匿名对象,很容易从中推断出他们不能或不想有一个具体的表示。尤其是当手头的问题只是向 json 添加一个键时。
  • @maccettura 您的回答将 OP 锁定为将对象相互关联,但我的回答并非如此。您也不知道 JSON 将用于什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多