来自datatable的嵌套json

时间:2013-03-26 02:55:18

标签: c# asp.net json linq json.net

我有一个数据表,其值如:

UerId    TimeStamp    Parameter    Value
-----    ---------    ---------    -----
  1      03/24/2013   Param1       Value1
  1      03/24/2013   Param2       Value2
  1      03/24/2013   Param3       Value3
  1      03/25/2013   Param4       Value4
  1      03/25/2013   Param5       Value5
  2      03/24/2013   Param1       Value6
  2      03/24/2013   Param2       Value7
  2      03/25/2013   Param1       Value8

我需要创建嵌套的json字符串,如:

Users:[
    "UserId": <id>,
    "date":[
         "TimeStamp": <TimeStamp>,
         "Values" : [
               { "Parameter": <Parameter>, "Value": <Value> },
           { "Parameter": <Parameter>, "Value": <Value> },
                       ...
        ] 
     ],
     ...
]

我制作了不同的值列表:

    List<String> ListOfUsers = new List<String>();
    List<DateTime> ListOfDates = new List<DateTime>();
    ListOfUsers = dt.AsEnumerable().Select(row => row.Field<String>("UserId")).Distinct().ToList<String>();
    ListOfDates = dt.AsEnumerable().Select(row => row.Field<DateTime>("TimeStamp").Date).Distinct().ToList<DateTime>();

我是否可以遍历整个表格,或者还有其他更好的方法,也许是使用Json.net库?感谢。

1 个答案:

答案 0 :(得分:0)

你可以做很多种方式,但最简单的方法是使用.net JavaScriptSerializer。只需构建类来模仿您感兴趣的结构,使用数据库中的数据填充对象,然后对其进行序列化。我会为你做一个例子。

将此作为您正在填充的对象

    public class YourObject
{
    public YourObject(){

    }

    public YourObject (DataTable dataTable){
        //Do work here to load your data set into your Users and other necessary objects.
        Users = new Users(dataTable);


    }

    public UsersObject Users { get; set; }

    public class UsersObject : List<UserObject> {
        public UsersObject (DataTable dataTable){
            dataTable.AsEnumerable().Select(row => row.Field<String>("UserId")).Distinct().ToList<String>().ForEach(x => this.Add(new UserObject(){UserId = x}));
            foreach(UserObject user in this){
                user.LoadDates(dataTable.Select("UserId = '" + user.UserId + "'"));
            }
        }
    }
    public class UserObject {
        public UserObject (){
            date = new DatesObject();
        }

        public void LoadDates(DataRow[] rows){
            rows.AsEnumerable().Select(row => row.Field<DateTime>("TimeStamp").Date).Distinct().ToList<DateTime>().ForEach(x => this.Add(new DateObject(){TimeStamp = x}));
            foreach(DateObject date in this){
                date.LoadParams(rows.Select("TimeStamp = '" + date.TimeStamp.ToString("MM/dd/yyyy") + "'"));
            }
        }

        public string UserId { get; set; }
        public DatesObject date { get; set; }
    }

    public class DatesObject : List<DateObject>{
        public DatesObject (){

        }
    }

    public class DateObject {
        public DateObject () {

        }

        public void LoadValues(DataRow[] rows){
            //Load your value/params like in the previous methods
        }

        public DateTime TimeStamp { get; set; }
        public ValuesObject Values { get; set; }
    }

    public class ValuesOject : List<ValueObject> {
        public ValuesOject () {

        }
    }

    public class ValueObject {
        public ValueObject () {

        }
        public string Parameter { get; set; }
        public string Value { get; set; }
    }
}   

然后使用它来序列化该对象

protected void Page_Load(object sender, EventArgs e){
     YourObject yourObject = new YourObject(Data.GetYourDataTable());
     JavaScriptSerializer serializer = new JavaScriptSerializer();
     string serializedItems = serializer.Serialize(yourObject);
}