Nested json from datatable

非 Y 不嫁゛ 提交于 2020-01-25 11:18:06

问题


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

I need to create nested json string like:

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

I made distinct list of values:

    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>();

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


回答1:


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


来源:https://stackoverflow.com/questions/15628569/nested-json-from-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!