繁体   English   中英

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

[英]How to convert datatable to json object

我的数据表格式如下

MCuserid Firsatname地址1地址2城市教育信息学院学位

1760 Rickert vbn addresstwo海得拉巴第二学院第二学位

第1766章死了

1766 Abhinav jhgjkhk测试地址BTech

我需要在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",
         }
        ]

}]

这里的问题是,在每一行DataTable是一组简单的名称/值对的,但你想每个人的EducationInfo在结果JSON另一层。 此外,你要多行成为一个JSON对象,如果所有字段,但 EducationInfo比赛。

不会有简单的“ Go()”方法可以直接为您执行此操作。

我建议创建StudentEducationInfo类来保存信息,然后将数据自己聚合到Student[]数组中。

然后,您只需使用自己喜欢的序列化器对其进行序列化。 副作用是,这使调试非常容易。

暂无
暂无

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

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