将mvc c#model类从datatable转换为json

时间:2018-03-11 18:29:36

标签: c# asp.net asp.net-mvc rest json.net

我有一个C#类和数据表。

数据表:

+---------+-----------------+---------------+---------+---------+--------------+
| Pers_Id | Pers_First_Name | Pers_Last_Name| OrderNu | OrderId |  Pers_Update |
+---------+-----------------+---------------+---------+---------+--------------+
| 1       |       ABC       |        Ln     |   76454 |  1      |   2018-03-25 |
+---------+-----------------+---------------+---------+---------+--------------+
| 1       |       ABC       |        Ln     |   76578 |  2      |   2018-03-25 |
+---------+-----------------+---------------+---------+---------+--------------+

类别:

public class Person
{
    public int Pers_Id { get; set; }
    public string Pers_First_Name { get; set; }
    public string Pers_Last_Name { get; set; }
    public DateTime Pers_Update { get; set; }
    public List<Order> Order_List { get; set; }

    public class Order
    {
        public int OrderID { get; set; }
        public string OrderNu { get; set; }
    }
}

我需要从数据表绑定此类,并需要将其转换为json对象,以便在asp .net web API中进行其他API响应。

当我绑定时,我得到json重复,但结果应该是这样的

{   “Pers_Id”:1,   “Pers_First_Name”:“ABC”,   “Pers_Last_Name”:“LN”,   “Pers_Update”:“”,   “订单”: [     {       “OrderID”:“1”,       “OrderNu”:“76454”     },     {       “OrderID”:“2”,       “OrderNu”:“76578”     }   ] }

2 个答案:

答案 0 :(得分:1)

当你有一个对象(在这个例子中是f.eks。你的Employee对象)时,你应该能够像这样返回它:

return Content(JsonConvert.SerializeObject(employee), "application/json");

此处有更多信息:https://stackoverflow.com/a/34091196/4034346

答案 1 :(得分:0)

首先;

  

using System.Web.Script.Serialization;

二; 如果您的数据表的类与您的Person类不同,那么您应该为您的人员创建一个新的数据表版本类。

    public class Person
    {
        public int Pers_Id { get; set; }
        public string Pers_First_Name { get; set; }
        public string Pers_Last_Name { get; set; }
        public DateTime Pers_Update { get; set; }
        public List<Order> Order_List { get; set; }
        public class Order
        {
            public int OrderID { get; set; }
            public int OrderNu { get; set; }
        }
    }


    //You need a class that fits to your DataTable
    public class PersonDataTable
    {
        public int Pers_Id { get; set; }
        public string Pers_First_Name { get; set; }
        public string Pers_Last_Name { get; set; }
        public DateTime Pers_Update { get; set; }
        public int OrderId { get; set; }
        public int OrderNu { get; set; }
    }

在你的方法中;

    public string ReturnGoodPeopleJsonFormat()
    {
        JavaScriptSerializer js = new JavaScriptSerializer();//Needed for converting an object to Json string.
        List<PersonDataTable> personDataTableList = new List<PersonDataTable>();//Needed for filling your data from in program or from database
        List<Person> personList = new List<Person>();//Needed 'to be converted' in to Json string


        //Add items to your DataTable list manually
        personDataTableList.Add(
            new PersonDataTable { Pers_Id = 1, Pers_First_Name = "ABC", Pers_Last_Name = "Ln", Pers_Update = Convert.ToDateTime("2018-03-25"), OrderId = 1, OrderNu = 76454 });
        personDataTableList.Add(
            new PersonDataTable { Pers_Id = 1, Pers_First_Name = "ABC", Pers_Last_Name = "Ln", Pers_Update = Convert.ToDateTime("2018-03-25"), OrderId = 2, OrderNu = 76578 });

        //or from database
        // personDataTableList.AddRange(myDatabaseModel.DataTables.ToList());



        //Now group your data by Pers_Id //We are grouping this because we don't want same person 2 or 3 time, we want one person just one time but get all orders in it. That's why we need to group them by Pers_Id
        foreach (var personGroup in personDataTableList.GroupBy(x => x.Pers_Id))
        {
            List<Person.Order> orderList = new List<Person.Order>();

            foreach (var dataTablePerson in personDataTableList.Where(x => x.Pers_Id == personGroup.Key))
            {
                //Get all orders of personGroup one by one in to an Order list from PersonDataTable list by using Pers_Id like a foreign key.
                ///This personGroup.Key is nothing but Pers_Id\\\ 
                orderList.Add(new Person.Order { OrderID = dataTablePerson.OrderId, OrderNu = dataTablePerson.OrderNu });
            }

            //Add new Person object to your personList if you don't have it before (by checking Pers_Id)
            if (personList.Where(x => x.Pers_Id == personGroup.Key).Count() == 0) //This personGroup.Key is nothing but Pers_Id
            {
                personList.Add(new Person
                {
                    Pers_Id = personDataTableList.Where(x => x.Pers_Id == personGroup.Key).FirstOrDefault().Pers_Id,
                    Pers_First_Name = personDataTableList.Where(x => x.Pers_Id == personGroup.Key).FirstOrDefault().Pers_First_Name,
                    Pers_Last_Name = personDataTableList.Where(x => x.Pers_Id == personGroup.Key).FirstOrDefault().Pers_Last_Name,
                    Pers_Update = personDataTableList.Where(x => x.Pers_Id == personGroup.Key).FirstOrDefault().Pers_Update,
                    Order_List = orderList
                });
            }

        }
        string JsonString = js.Serialize(personList);
        return JsonString;
    }

结果如下:

  

[{ “Pers_Id”:1, “Pers_First_Name”: “ABC”, “Pers_Last_Name”: “LN”, “Pers_Update”: “/日期(1521925200000)/”, “Order_List”:[{ “订单ID”: 1, “OrderNu”:76454},{ “订单ID”:2 “OrderNu”:76578}]}]