列表<idictionary <string,object =“”>&gt;添加更多List <idictionary <string,object =“”>&gt;标题

时间:2017-06-23 18:06:58

标签: c# json list json.net idictionary

嘿所有我想添加(或合并,如果你想称之为)我创建的一些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输出看起来像这样:

目前我只是返回名为List< IDictionary< string, object>>的1 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< 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...
        }
    ]
 }

enter image description here

2 个答案:

答案 0 :(得分:1)

你想要的是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。

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

答案 1 :(得分:0)

你在问这样的事吗?

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