c# – 将列表序列化为谷歌图表json数据表

前端之家收集整理的这篇文章主要介绍了c# – 将列表序列化为谷歌图表json数据表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种将对象列表序列化为谷歌图表json数据格式的通用方法.

This示例非常接近,但它使用的是数据表..

我希望这会涉及一些反思,有些可能是模型属性内容.
谁能指点我到图书馆或什么的?

如果我可以将这样的查询序列化为谷歌图表格式,那就更好了:

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

解决方法

我将创建自己的类层次结构,与Google的API匹配,然后使用JSON.NET对其进行序列化.可能的数据模型:
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
}

然后是一个示例用法

var graph = new Graph {
    cols = new ColInfo[] {
        new ColInfo { id = "A",label = "set A",type = "string" },new ColInfo { id = "B",label = "set B",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();

您可以使用Linq的Select()将数据转换为Google的数据模型,然后将其序列化为JSON.

原文链接:https://www.f2er.com/csharp/98408.html

猜你在找的C#相关文章