Serialize a <T> list in google charts json datatable

I am looking for a general way to serialize a list of objects in json data from google diagrams.

This example is pretty close, but it uses datatable ..

I expect this to lead to some reflection, and some may start to model properties. Can someone point me to a library or something else?

It would be even better if I could serialize such a request in google chart format:

var results = from m in be.cmsMember where m.FirstLogin != null && m.FirstLogin >= BitCoinGoLive group m by new { Year = m.FirstLogin.Value.Year, Month = m.FirstLogin.Value.Month, Day = m.FirstLogin.Value.Day } into grp select new { Year = grp.Key.Year, Month = grp.Key.Month, Day = grp.Key.Day, Count = grp.Count() }; 
+6
source share
5 answers

I would create my own class hierarchy that matches the Google APIs and then use JSON.NET to serialize it. Possible data model:

 public class Graph { public ColInfo[] cols { get; set; } public DataPointSet[] rows { get; set; } public Dictionary<string, string> p { get; set; } } public class ColInfo { public string id { get; set; } public string label { get; set; } public string type { get; set; } } public class DataPointSet { public DataPoint[] c { get; set; } } public class DataPoint { public string v { get; set; } // value public string f { get; set; } // format } 

Then an example of use:

 var graph = new Graph { cols = new ColInfo[] { new ColInfo { id = "A", label = "set A", type = "string" }, new ColInfo { id = "B", label = "set B", type = "string" }, new ColInfo { id = "C", label = "set C", type = "string" } }, rows = new DataPointSet[] { new DataPointSet { c = new DataPoint[] { new DataPoint { v = "a" }, new DataPoint { v = "b", f = "One" } } } }, p = new Dictionary<string, string>() }; string json; //var s = new JsonSerializer(); var s = new JavaScriptSerializer(); /*using (var sw = new StringWriter()) { s.Serialize(sw, graph); json = sw.ToString(); }*/ var sw = new StringBuilder(); s.Serialize(graph, sw); json = sw.ToString(); 

You can use Linq Select () to transform the data into a Google data model, and then serialize it to JSON.

+16
source

Take a look at JSON.NET . This is a great library for generating json based on your class, this page will give you serialization examples: http://james.newtonking.com/pages/json-net.aspx .

Hope this points you in the right direction

+1
source

Here is the full working functionality, which also works with anonymous types.

Pay attention to the dates: they should also be analyzed on the client: for example,

 for (var i = 0; i < data.rows.length;i++ ) { data.rows[i].c[0].v = new Date(data.rows[i].c[0].v); } private string getGetJsonString<T>(IEnumerable<dynamic> list, dynamic row) { string header = "{\"cols\":["; PropertyInfo[] props = row.GetType().GetProperties(); foreach (PropertyInfo p in props) { header += "{\"id\":\"" + p.Name + "\", \"label\":\"" + p.Name + "\","; switch (p.PropertyType.Name) { case "Int32": header += "\"type\":\"number\""; break; case "DateTime": header += "\"type\":\"date\""; break; default: header += "\"type\":\"string\""; break; } header += "},"; } header = header.Substring(0, header.Length - 1); header += "]"; StringBuilder json = new StringBuilder(); json.Append(header + ",\"rows\":["); bool first = true; foreach (dynamic a in list) { string jRow = "{\"c\":["; if (first) first = false; else jRow = "," + jRow; foreach (PropertyInfo p in props) { // todo get other fieldtypes from http://code.google.com/apis/chart/interactive/docs/reference.html#dataparam switch (p.PropertyType.Name) { case "Int32": jRow += "{\"v\":"; jRow += p.GetValue(a,null).ToString(); jRow += "},"; break; case "DateTime": jRow += "{\"v\":\""; DateTime d = ((DateTime)p.GetValue(a, null)); //jRow += d.DayOfYear; //jRow += "\\/Date("+d.Ticks+")\\/"; jRow += d.ToString("yyyy-MM-dd"); //jRow += "new Date(" + d.Ticks+ ")"; jRow += "\"},"; break; default: jRow += "{\"v\":\""; jRow += p.GetValue(a,null).ToString(); jRow += "\"},"; break; } } jRow = jRow.Substring(0, jRow.Length - 1); json.Append(jRow + "]}"); } json.Append("]}"); return json.ToString() ; } 
+1
source

I recommend using a combination of classes that conform to the google JSON format for diagrams in combination with Json.net. The main steps.

  • Specify Columns
  • Select your data from any source (using linq) in the row cell values ​​corresponding to the correct columns.
  • serialize to json using JSON.net

This is a combination of two sentences from @emfurry and @ TWith2Sugars.

So, classes similar to @emfurrys in place (maybe add some constructors to remove the need to initialize the object) Something like:

 Table table = new Table(); ColInfo[] cols = new ColInfo[4]; cols.Add("year", "Year", "string"); cols.Add("month", "Month", "string"); cols.Add("day", "Day", "string"); cols.Add("count", "Count", "number"); table.rows = cmsMembersWithCount.Select(row => new DataPointSet(){ {new DataPoint(row.Year)} {new DataPoint(row.Month)} {new DataPoint(row.Day)} {new DataPoint(row.Count)} }).ToArray(); var json = JsonConvert.SerializeObject(table); 

Bob is your uncle. NB Bob is untested, he is here as a small example, he takes a fair bit, but he, hopefully, gives a brief description of how to quickly switch from any data source to the Google chart JSON format.

0
source

I am using Google DataTable.NET Wrapper , available on Nuget . I have a short article about this on my blog at http://nicholasbering.ca/dot-net/2014/10/24/google-data-table-dot-net-wrapper/ , but to summarize, you can do something like the following.

 var list = new[] { new {Name = "Dogs", Count = 5}, new {Name = "Cats", Count = 2} }; var json = list.ToGoogleDataTable() .NewColumn(new Column(ColumnType.String, "Name"), x => x.Name) .NewColumn(new Column(ColumnType.Number, "Count"), x => x.Count) .Build() .GetJson(); 

The above example works on an array, but it should work with any IEnumrable<T> .

0
source

Source: https://habr.com/ru/post/889181/


All Articles