如何从datatable构建json字符串 [英] How to build json string from datatable

查看:116
本文介绍了如何从datatable构建json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helo everyone



我对数据表中的数据有问题。我的数据是这样的。



Helo everyone

I have a problem with data from datatable. My data like that.

ID      Name    Parent
1       A       0
2       B       1
3       C       1
4       D       1
5       E       3
6       F       3
7       G       2
8       H       4
9       I       5
10      J       3





请帮帮我从这个数据表构建json字符串:





Please help me to build json string from this datatable like that:

[
 {'ID: '1'; 'Name: 'A'; 'Child': [{'ID': '2'; 'Name': 'B'; 'Child': [{'ID: '7'; 'Name: 'G'; 'Child': 'null'}]}, {.....}]}
 ]





我试着想想那些日子,但是我找不到解决问题的方法。



请问我是否有问题要问我我是



PS:Sory,我的英语不好。



非常感谢

Nguyen



I try hare to think about that some days but I can't find the way to resolve it.

Please ask me if you have a question to me

PS: Sory, my English is not good.

Many thank
Nguyen

推荐答案

我相信谷歌已经失败了'有些日子'......

http://www.newtonsoft.com /json/help/html/serializingjson.htm [ ^ ]
I believe Google were down for 'some days'...
http://www.newtonsoft.com/json/help/html/serializingjson.htm[^]


点击此链接: 3种方式将数据表转换为Asp.net C#中的JSON [ ^ ]


首先按列对数据表进行排序父母。



你必须手动循环遍历行并使用StringBuilder形成一个json字符串。



遍历每一行并根据格式添加到Str ingBuilder对象并循环通过其余可用行来匹配id与Parent列。如果找到,则将其添加到构建器。



一个简单示例 - http://weblogs.asp.net/navaidakhtar/converting-data-table-dataset-into-json-string [ ^ ]



但是在这个方法中你需要添加更多逻辑来添加另一个循环来读取当前行作为父级的所有其他行并将其添加到构建器。
First sort the data table by the column "Parent".

You have to manually loop through the rows and form a json string using StringBuilder.

Loop through each row and add according to the format to a StringBuilder object and loop through rest of the rows available to match the id with Parent column. If found, then add that to the builder.

One simple example - http://weblogs.asp.net/navaidakhtar/converting-data-table-dataset-into-json-string[^]

But inside this method you need to add more logic to add another loop to read all other rows which have current row as parent and add that to the builder.


这篇关于如何从datatable构建json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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