从数据表嵌套的json

从数据表嵌套的json

本文介绍了从数据表嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a datatable with values like:

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字符串,例如:

I need to create nested json string like:

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库?谢谢.

Sould I loop through whole table or there is any other better way, perhaps with Json.net library? Thanks.

推荐答案

您可以通过多种方式执行此操作,但最简单的方法是使用.net JavaScriptSerializer.只需构建您的类以模仿您感兴趣的结构,然后使用数据库中的数据填充对象,然后对其进行序列化即可.我帮你做个例子.

You can do this many ways, but the simplest would be through the use if the .net JavaScriptSerializer. Just build your classes to mimic the structure you're interested in, populate the object with your data from the database, then serialize it. I'll work up an example for you.

将此用作您要填充的对象

Use this as the object that you are populating

    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; }
    }
}

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

Then use this to serialize that object

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

这篇关于从数据表嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:19