C# 列表添加更多带有标题的列表
嘿,我正在尝试添加或合并的所有内容,如果您想将其称为我创建的一些列表,如下所示:C# 列表添加更多带有标题的列表,c#,json,list,json.net,idictionary,C#,Json,List,Json.net,Idictionary,嘿,我正在尝试添加或合并的所有内容,如果您想将其称为我创建的一些列表,如下所示: List<IDictionary<string, object>> eachTblColumnProperties = new List<IDictionary<string, object>> { new Dictionary<string, object> {{ "title", "Type" },{"name", "Type" },{ "
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...........
};
{
"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...
}
]
}
就像:
{
"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>> 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...........
};
{
"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>>> 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
编辑忘记在列表前添加新内容。工作小提琴你的问题是什么?我想你是想把你所有的字典名称作为它们所保存数据的键。如果是这样的话,那就用另一个字典存储名称作为键,值就是字典,它与声音的关联就像字典一样,更符合你的需要。漂亮的卡米洛!谢谢你。这看起来正是我所需要的。@StealthRT我投了反对票,因为您在示例中使用了匿名类,这个答案生成了一个具体的类,所有这些都是为了序列化。它的杀伤力和限制性。@maccettura的OP状态,他们希望它是动态的吗?不,行动组有没有说他不想要具体的类型?不可以。这也可以在没有类的情况下完成,使用匿名类型,但记住不同方法中的结构会很糟糕。@Camiloteriento OP显然使用了匿名对象,很容易推断出他们不能或不希望有具体的表示。尤其是当眼前的问题只是向json添加一个键时。@maccettura您的答案会锁定OP,使其能够放置彼此相关的对象,但我的答案并非如此。你不知道JSON会被用来做什么这也很好,Maccettura。谢谢哼我收到了很多红色的代码。请参阅OP中的图片。@StealthRT修复了它并添加了一个提琴。谢谢maccettura。效果很好。