分层JSON数据到分层表

时间:2022-07-22 14:55:17

I have a class like this and to this type I need to deserialize a JSON string

我有一个这样的类,对于这种类型,我需要反序列化一个JSON字符串

public class nodes
{
    public int id{get;set;}
    public string name{get;set;}
    public List<nodes> children{get;set;}
}

and the JSON is like this

而JSON就是这样的

{
  id: 15,
  name: 'user1',
  children: [
    {
      id: 22,
      name: 'user2',
      children: [
        {
          id: 34,
          name: 'user3',
          children: [
            {
              id: 43,
              name: 'user4',
              children: []
            },
            {
              id: 54,
              name: 'user5',
              children: []
            }
          ]
        },
        {
          id: 65,
          name: 'user6',
          children: []
        }
      ]
    },
    {
      id: 72,
      name: 'user7',
      children: []
    }
  ]
}

This is how I'm deserialising

这就是我反序列化的方式

node d=JsonConvert.DeserializeObject<node>(myJSON); //myJSON is my above JSON string

But the requirement is I need to insert these data into an SQL table as separate rows. The table should is as follows

但要求是我需要将这些数据作为单独的行插入到SQL表中。表格如下

UniqueID    id    name    ParentID
------------------------------------
    1       15    User1     0
    2       22    User2     1
    3       34    User3     2
    4       43    User4     3
    5       54    User5     3
    6       65    User6     2
    7       72    User7     1
--------------------------------------

As you see the table there is a system generated ID column UniqueID. Also another column ParentID to keep the hierarchy..

如您所见,表中有一个系统生成的ID列UniqueID。还有另一列ParentID来保持层次结构..

I can ofcourse use some recursive functions to handle each children to their details and create a dynamic query to insert. But I dont think its a best solution. Please suggest a better way to do this

我可以使用一些递归函数来处理每个子节点的细节,并创建一个动态查询来插入。但我不认为这是一个最好的解决方案。请建议一个更好的方法来做到这一点

1 个解决方案

#1


2  

How about flattening your tree via Linq (it is still going to use recursion), I think it may make it clearer. You could then just loop throught the result. I'm thinking something like this:

怎么样通过Linq展平你的树(它仍然会使用递归),我认为这可能会让它更清晰。然后你可以循环遍历结果。我在想这样的事情:

First write an extension method:

首先编写一个扩展方法:

public static IEnumerable<T> Flatten<T>(
    this IEnumerable<T> c,
    Func<T,IEnumerable<T>> f) 
{
    return c.SelectMany(n => f(n).Flatten(f)).Concat(c);
}

Then you can flatten and loop:

然后你可以展平并循环:

var nodesList = nodes.Flatten(node => node.children).ToList();

foreach(var n in nodeList)
{
    /add to db
}

#1


2  

How about flattening your tree via Linq (it is still going to use recursion), I think it may make it clearer. You could then just loop throught the result. I'm thinking something like this:

怎么样通过Linq展平你的树(它仍然会使用递归),我认为这可能会让它更清晰。然后你可以循环遍历结果。我在想这样的事情:

First write an extension method:

首先编写一个扩展方法:

public static IEnumerable<T> Flatten<T>(
    this IEnumerable<T> c,
    Func<T,IEnumerable<T>> f) 
{
    return c.SelectMany(n => f(n).Flatten(f)).Concat(c);
}

Then you can flatten and loop:

然后你可以展平并循环:

var nodesList = nodes.Flatten(node => node.children).ToList();

foreach(var n in nodeList)
{
    /add to db
}