我想创建嵌套的JSON。我从sql表中获取数据。 表格中的数据格式:
+---------------------+---------+
| Domain | Action |
+---------------------+---------+
| Keyword | New |
| Keyword | Edit |
| Keyword | Delete |
| Keyword.Case | New |
| Keyword.Case | Edit |
| Keyword.Case | Delete |
| Keyword.Case.Action | New |
| Keyword.Case.Action | Edit |
| Keyword.Case.Action | Delete |
| User | New |
| User | Edit |
| User | Delete |
+---------------------+---------+
我使用linq查询从sql表中检索了数据。现在我想解析它以获得下面提到的层次结构。 我认为数据应采用以下格式:
[
{
"Domain": "Keyword",
"Actions": ["New", "Edit", "Delete"],
"Nodes": [
{
"Domain": "Keyword.Case",
"Actions": ["New", "Edit", "Delete"],
"Nodes": [
{
"Domain": "Keyword.Case.Action",
"Actions": ["New", "Edit", "Delete"],
"Nodes": []
}
]
}
],
{
"Domain": "User",
"Actions": ["New", "Edit", "Delete"],
"Nodes": []
}
]
Dot(。)表示它是子节点。 例如keyword.case.action;在这个示例中,关键字是Case的父级,Case是Action的父级,意味着action是case的子级,case是关键字的子级。我想要这样的东西:
有人可以帮忙吗?提前致谢
答案 0 :(得分:0)
使用LinQ检索SQL数据时,应该有一个IEnumerable结果。您可以直接序列化:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonOutput = serializer.Serialize(dbResult);
其中dbResult
是您在LinQ-Operation