首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中将层次字符串列表转换为JSON

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

Stack Overflow用户
提问于 2012-10-18 02:54:12
回答 1查看 452关注 0票数 1

我有一个表示分层数据的字符串列表,它们是用连字符分隔的(3个连字符表示分隔)。我正在尝试将此列表转换为JSON字符串,以便可以将其绑定到树控件。

我正在寻找一个C#的例子。

列表可以如下所示(列表并不是完整的列表,在某些情况下它可以有7个节点,但你可以理解它的意思):

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2012-10-18 05:57:44

主要的技巧是将字符串放入树结构中。接下来的代码片段(linqpad)可以做到这一点。我不知道输出是不是可以在客户端处理,但它应该是您可以根据自己的需要进行修改的东西。

代码语言:javascript
复制
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; }
}

输出:

[{“名称”:“汽车电子”,“节点”:[{“名称”:“车身电子”,“节点”:[{“名称”:“门禁系统”,“节点”:[]},{“名称”:“车身控制模块”,“节点”:[]}]},{“名称”:“驾驶员信息”,“节点”:[{“名称”:“时钟”,“节点”:[]},{“名称”:“指南针系统”,"Nodes":[]}]},{"Name":"HomeL","Nodes":[]},{“Name”:“信息娱乐与连接”,“Nodes”:[{“Name”:“免提系统”,"Nodes":[]},{“Name”:“汽车内饰”,“Nodes”:[{“Name”:“门板”,"Nodes":[]},{“Name”:“地板控制台”,“Nodes”:[]},{“名称”:“车顶和架空系统”,“节点”:[]},{“名称”:“架空控制台”,“节点”:[]}},{“名称”:“汽车座椅”,“节点”:[{“名称”:“完整座椅”,“节点”:[{“名称”:“SuperThin座椅”,“节点”:[]}}]

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12941305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档