将具有层次结构的C#数据表序列化为json heirachy [英] serialise a c# datatable with a hierarchy as json heirachy

查看:304
本文介绍了将具有层次结构的C#数据表序列化为json heirachy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dataTable,其中包含以下数据:

I have a dataTable with the following data in it:

Parent  Child
Dan     Heidi
Dan     Lauren
Alan    Dan
Daphne  Alan
Alan    Lorna
Alan    Tim

我要输出以下json:

I want to output the following json:

[
  {
    'name': 'Daphne',
    'children': [
      {
        'name': 'Alan',
        'children': [
          {
            'name': 'Dan',
            'children': [
              {
                'name': 'Heidi'
              },
              {
                'name': 'Lauren'
              }
            ]
          },
          {
            'name': 'Tim'
          },
          {
            'name': 'Lorna'
          }
        ]
      }
    ]
  }
]

我已经使用json.net序列化程序来序列化层次结构中的一个级别,我会认为这是一个常见问题.有没有简单的方法可以将具有层次结构的datTable转换为具有层次结构的json字符串?

I've used the json.net serialiser to serialse one level in the hierarchy, I would have thought this was a common problem. Is there a simple way to convert a datTable with a hierarchy to a json string with a hierarchy?

我想关键是要确定层次结构中有多少个层次,我看了sql server中的architectureid数据类型,但似乎有点麻烦.

I guess the key bit is identifying how many and which levels there are in the hierarchy, ive looked at the hierarchyid datatype in sql server but it seems a bit messy to need that.

谢谢, 丹

推荐答案

下面是示例或者您可以从dataTable创建IEnumerable<ComplexData>并序列化

Or you can create IEnumerable<ComplexData> from dataTable and serialise

这篇关于将具有层次结构的C#数据表序列化为json heirachy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆