簡體   English   中英

在C#中將層級字符串列表轉換為JSON

[英]Convert a list of hierachical strings to JSON in C#

我有一個表示層次結構數據的字符串列表,這些字符串用連字符分隔(3個連字符表示分隔)。 我正在嘗試將此列表轉換為JSON字符串,以便可以將其綁定到樹控件。

我正在尋找一個C#示例。

列表可以如下(列表不是完整列表,在某些情況下,它可以有7個節點深度,但是您可以理解):

Automotive Electronics
Automotive Electronics---Body Electronics
Automotive Electronics---Body Electronics---Access Control Systems
Automotive Electronics---Body Electronics---Body Control Modules
Automotive Electronics---Driver Information
Automotive Electronics---Driver Information---Clocks
Automotive Electronics---Driver Information---Compass Systems
Automotive Electronics---HomeL
Automotive Electronics---Infotainment & Connectivity
Automotive Electronics---Infotainment & Connectivity---Handsfree Systems
Automotive Interiors
Automotive Interiors---Door Panels
Automotive Interiors---Floor Consoles
Automotive Interiors---Headliners & Overhead Systems
Automotive Interiors---Overhead Consoles
Automotive Seating
Automotive Seating---Complete Seats
Automotive Seating---Complete Seats---SuperThin Seats

主要技巧是將字符串變成樹形結構。 下一個代碼片段(linqpad)做到了。 我不知道您是否可以按原樣輸出輸出,但是應該根據自己的需要進行修改。

void Main()
{
    var rootNode = new Node("root");
    foreach(string s in new[] {"Automotive Electronics",
        "Automotive Electronics---Body Electronics",
        "Automotive Electronics---Body Electronics---Access Control Systems",
        "Automotive Electronics---Body Electronics---Body Control Modules",
        "Automotive Electronics---Driver Information",
        "Automotive Electronics---Driver Information---Clocks",
        "Automotive Electronics---Driver Information---Compass Systems",
        "Automotive Electronics---HomeL",
        "Automotive Electronics---Infotainment & Connectivity",
        "Automotive Electronics---Infotainment & Connectivity---Handsfree Systems",
        "Automotive Interiors",
        "Automotive Interiors---Door Panels",
        "Automotive Interiors---Floor Consoles",
        "Automotive Interiors---Headliners & Overhead Systems",
        "Automotive Interiors---Overhead Consoles",
        "Automotive Seating",
        "Automotive Seating---Complete Seats",
        "Automotive Seating---Complete Seats---SuperThin Seats"})
    {
        AddNodes(rootNode, s.Split(new[] {"---"}, StringSplitOptions.RemoveEmptyEntries));
    }
    new JavaScriptSerializer().Serialize(rootNode.Nodes).Dump();
}

public void AddNodes( Node parentNode, string[] names)
{
    if (names.Any())
    {
        var node = parentNode.AddNode(names.First());
        AddNodes(node, names.Skip(1).ToArray());
    }
}

public class Node
{
    public Node(string name)
    {
        Name = name;
        Nodes = new List<Node>();
    }

    public Node AddNode(string name)
    {
        if (!this.Nodes.Select(n => n.Name).Contains(name.Trim()))
        {
            //name.Dump(this.Name);
            this.Nodes.Add(new Node(name.Trim()));
        }
        return this.Nodes.Where (n => n.Name == name).First();
    }

    public string Name { get;set;}
    public List<Node> Nodes { get; set; }
}

輸出:

[{“名稱”:“汽車電子”,“節點”:[{“名稱”:“車身電子”,“節點”:[{“名稱”:“訪問控制系統”,“節點”:[]}, {“名稱”:“車身控制模塊”,“節點”:[]}]},{“名稱”:“駕駛員信息”,“節點”:[{“名稱”:“時鍾”,“節點”:[ ]},{“名稱”:“指南針系統”,“節點”:[]}]},{“名稱”:“ HomeL”,“節點”:[]},{“名稱”:“信息娛樂和連接” ,“節點”:[{“名稱”:“免提系統”,“節點”:[]}]}]}},{“名稱”:“汽車內飾”,“節點”:[{“名稱”:“門面板”,“節點”:[]},{“名稱”:“地板控制台”,“節點”:[]},{“名稱”:“頂篷和頂置系統”,“節點”:[]},{ “名稱”:“高架控制台”,“節點”:[]}]},{“名稱”:“汽車座椅”,“節點”:[{“名稱”:“完全座位”,“節點”:[{ “名稱”:“超薄座椅”,“節點”:[]}]}]}}]

(請注意,對於JavaScriptSerializer您必須在linqpad中導入一些名稱空間才能運行此代碼段)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM