[英]Parse data to nested JSON
我想創建嵌套的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": []
}
]
點(。)表示它是子節點。 例如keyword.case.action; 在此示例中,關鍵字是Case的父級,而Case是Action的父級,表示action是case的子級,而case是關鍵字的子級。 我想要這樣的東西:
有人可以幫忙嗎? 提前致謝
當您使用LinQ檢索SQL數據時,您應該得到IEnumerable結果。 您可以直接將其序列化:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonOutput = serializer.Serialize(dbResult);
其中dbResult
是您在LinQ-Operation之后檢索到的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.