简体   繁体   中英

Convert a list of hierachical strings to JSON in C#

I have a list of strings that represent hierarchical data and are hyphen-separated (3 hyphens denote separation). I'm trying to convert this list into a JSON string so that I can bind it to a tree control.

I am looking for a C# example.

The list can be as follow (list is not the complete list and in some cases it can have 7 nodes deep, but you can get the idea):

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

The main trick is to get the strings into a tree structure. The next code snippet (linqpad) does that. I don't know if the output as-is is something you can handle client-side, but it should be something you can modify as it suits you.

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; }
}

Output:

[{"Name":"Automotive Electronics","Nodes":[{"Name":"Body Electronics","Nodes":[{"Name":"Access Control Systems","Nodes":[]},{"Name":"Body Control Modules","Nodes":[]}]},{"Name":"Driver Information","Nodes":[{"Name":"Clocks","Nodes":[]},{"Name":"Compass Systems","Nodes":[]}]},{"Name":"HomeL","Nodes":[]},{"Name":"Infotainment & Connectivity","Nodes":[{"Name":"Handsfree Systems","Nodes":[]}]}]},{"Name":"Automotive Interiors","Nodes":[{"Name":"Door Panels","Nodes":[]},{"Name":"Floor Consoles","Nodes":[]},{"Name":"Headliners & Overhead Systems","Nodes":[]},{"Name":"Overhead Consoles","Nodes":[]}]},{"Name":"Automotive Seating","Nodes":[{"Name":"Complete Seats","Nodes":[{"Name":"SuperThin Seats","Nodes":[]}]}]}]

(Note that for the JavaScriptSerializer you have to import some namespaces in linqpad to run this snippet).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM