简体   繁体   English

如何将数据表转换为JSON对象

[英]How to convert datatable to json object

My datatable is in below format 我的数据表格式如下

MCuserid Firsatname address1 address2 city educationinfo institute degree MCuserid Firsatname地址1地址2城市教育信息学院学位

1760 Rickert vbn addresstwo hyderabad Second College Second Degree 1760 Rickert vbn addresstwo海得拉巴第二学院第二学位

1766 Abhinav jhgjkhk testaddress mtech 第1766章死了

1766 Abhinav jhgjkhk testaddress BTech 1766 Abhinav jhgjkhk测试地址BTech

I need output in following json format in c# 我需要在C#中以以下json格式输出

[

{
    "MCUserID" : 1760,
    "FirstName": "Rickert",
    "Address1" : "vbn",
    "Address2" : "address two",
    "city      : "hyderabad"
    "EducationInfo": [
        {
           Institute: "Second College",
           Degree: "Second Degree",
         }
]
},
{    
  "MCUserID"   : 1766,
    "FirstName": "Abhinav",
    "Address1" : "jhgjkhk",
    "Address2" : " test address",
    "city      : ""
    "EducationInfo": [
        {
           Institute: "",
           Degree: "mtech",
         },
         {
           Institute: "",
           Degree: "btech",
         }
        ]

} ] }]

The problem here is that each row in the DataTable is a simple set of name/value pairs, but you want each person's EducationInfo as another layer in the result JSON. 这里的问题是,在每一行DataTable是一组简单的名称/值对的,但你想每个人的EducationInfo在结果JSON另一层。 Furthermore you want multiple rows to become a single JSON object if all fields but EducationInfo match. 此外,你要多行成为一个JSON对象,如果所有字段,但 EducationInfo比赛。

There isn't going to be a simple "Go()" method that will do this for you out of the box. 不会有简单的“ Go()”方法可以直接为您执行此操作。

I suggest creating Student and EducationInfo classes to hold the information, and aggregate the data yourself into a Student[] array. 我建议创建StudentEducationInfo类来保存信息,然后将数据自己聚合到Student[]数组中。

Then you simply serialize them using your favorite serializer. 然后,您只需使用自己喜欢的序列化器对其进行序列化。 As a side effect, this makes debugging so much easier. 副作用是,这使调试非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM