【发布时间】:2017-11-27 09:28:07
【问题描述】:
嘿,我正在尝试添加(或合并,如果你想这样称呼它)我创建的几个 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 输出如下所示:
目前我只返回 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...
}]
}
这很好用,但我需要能够在一个回电中发送我所有的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...
}
]
}
【问题讨论】:
-
你有什么问题?
-
我认为您正在尝试将所有字典名称作为它们所保存数据的键。如果是这种情况,只需另一个字典将名称存储为键,值是与其关联的字典
-
听起来
Dictionary<string, List<Dictionary<string, string>>>更符合您的要求。
标签: c# json list json.net idictionary