【问题标题】:Convert a list of hierachical strings to JSON in C#在 C# 中将分层字符串列表转换为 JSON
【发布时间】:2012-10-08 02:57:43
【问题描述】:

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

【问题讨论】:

  • 这个数据会经常变化吗?你会输入这个还是从其他来源提取它?
  • 它来自一个数据库,所以它有可能改变......我基本上是把这些作为一个列表从数据库中拉出来
  • 好吧,它们会以分层的形式出现吗?
  • 他们会完全按照我上面的列表进来......也就是说,已经按顺序排列了,并且父母总是在孩子之前。
  • 您的预期输出是什么?我假设您希望它采用某种格式(您未在此处提供)。

标签: c# json string hierarchical delimited


【解决方案1】:

主要技巧是将字符串放入树结构中。下一个代码 sn-p (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":"车身控制模块","Nodes":[]}]},{"Name":"Driver Information","Nodes":[{"Name":"Clocks","Nodes ":[]},{"Name":"Compass Systems","Nodes":[]}]},{"Name":"HomeL","Nodes":[]},{"Name":"信息娱乐& Connectivity","Nodes":[{"Name":"Handsfree Systems","Nodes":[]}]}]},{"Name":"Automotive Interiors","Nodes":[{"Name" :"门板","节点":[]},{"名称":"地板控制台","节点":[]},{"名称":"顶篷和顶置系统","节点":[] },{"Name":"Overhead Consoles","Nodes":[]}]},{"Name":"Automotive Seating","Nodes":[{"Name":"Complete Seats","Nodes" :[{"Name":"SuperThin Seats","Nodes":[]}]}]}]

(请注意,对于JavaScriptSerializer,您必须在 linqpad 中导入一些命名空间才能运行此 sn-p)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2011-06-26
    • 2017-02-04
    相关资源
    最近更新 更多