在C#中将分层字符串列表转换为JSON

时间:2012-10-17 18:54:12

标签: c# json string hierarchical delimited

我有一个表示分层数据的字符串列表,并且是连字符分隔的(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

1 个答案:

答案 0 :(得分:0)

主要技巧是将字符串转换为树结构。下一个代码片段(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; }
}

输出:

  

[{“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” “:[]},{”名称“:”指南针系统“,”节点“:[]}]},{”名称“:”HomeL“,”节点“:[]},{”名称“:”信息娱乐系统&amp; Connectivity“,”Nodes“:[{”Name“:”Handsfree Systems“,”Nodes“:[]}]}]},{”Name“:”Automotive Interiors“,”Nodes“:[{”Name “:”门面板“,”节点“:[]},{”名称“:”楼层控制台“,”节点“:[]},{”名称“:”头条新闻和架空系统“,”节点“: []},{“名称”:“开销控制台”,“节点”:[]}]},{“名称”:“汽车座位”,“节点”:[{“名称”:“完整座位”,“节点“:[{”Name“:”SuperThin Seats“,”Nodes“:[]}]}]}]

(请注意,对于JavaScriptSerializer,您必须在linqpad中导入一些名称空间以运行此代码段。)