List<IDictionary<string, object>> 添加更多带有标题的 List<IDictionary<string, object>>

List<IDictionary<string, object>> adding more List<IDictionary<string, object>> with title

嘿,我正在尝试添加(或者合并,如果你想这样称呼它的话)一些我创建的 List<IDictionary<string, object>>,看起来像这样:

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< IDictionary< string, object>> 想变成 JSON(使用 json.net)。

所以它们的 JSON 输出看起来像这样:

目前我正在 returning 1 List< IDictionary< string, object>> 调用 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...
 }]
}

这很好用,但我需要能够在那个 return 调用中发送我所有的 List< IDictionary< string, object>>...

所以看起来像这样:

 {
    "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...
        }
    ]
 }

你要这样的东西吗?

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);

你要的是Dictionary<string, List<Dictionary<string, object>>>

示例:

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。工作 fiddle here