繁体   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