微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/98408.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐