如何使用 C# 创建 Google DataTable JSON 预期源?

发布于 2024-08-23 15:51:25 字数 693 浏览 5 评论 0 原文

如何创建 google.visualization.datatable 所需的 JSON 源使用 C#?显然,使用 JavaScriptSerializer 是不可能的,因为预期的 JSON 具有文档中描述的奇怪结构:

var dt = new google.visualization.DataTable(
     {
       cols: [{id: 'task', label: 'Task', type: 'string'},
                {id: 'hours', label: 'Hours per Day', type: 'number'}],
       rows: [{c:[{v: 'Work'}, {v: 11}]},
              {c:[{v: 'Eat'}, {v: 2}]},
              {c:[{v: 'Commute'}, {v: 2}]},
              {c:[{v: 'Watch TV'}, {v:2}]},
              {c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
             ]
     },
   0.6
)

How can I create the JSON source expected by the google.visualization.datatable using C#? Obviously using the JavaScriptSerializer is out of the question since the expected JSON has a weird structure as described on the documentation:

var dt = new google.visualization.DataTable(
     {
       cols: [{id: 'task', label: 'Task', type: 'string'},
                {id: 'hours', label: 'Hours per Day', type: 'number'}],
       rows: [{c:[{v: 'Work'}, {v: 11}]},
              {c:[{v: 'Eat'}, {v: 2}]},
              {c:[{v: 'Commute'}, {v: 2}]},
              {c:[{v: 'Watch TV'}, {v:2}]},
              {c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
             ]
     },
   0.6
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞旧 2024-08-30 15:51:25

虽然我不熟悉 .NET 环境,但 Google Visualization API 有一个名为 bortosky-google-可视化。该库从 System.Data.DataTable 对象写入 JSON Google DataTable。

Though I'm not familiar in the .NET environment, there is a .NET helper for the Google Visualization API called bortosky-google-visualization. The library writes a JSON Google DataTable from a System.Data.DataTable object.

爱情眠于流年 2024-08-30 15:51:25

实现此目的的另一种方法是使用 Google DataTable .Net Wrapper (https://googledatatablelib.codeplex.com/) 它提供了使用强类型 System.DataTable 的可能性,然后可以将其转换为 google.datatable 可视化 JSON 格式。

此服务器端代码

public string GetStatisticsForChart(string messageCode)
{
    //some repository that returns data....
    var data = _statisticsRepository.GetPerMessage(messageCode);

    //It simply returns a list of objects with Year and Count properties.
    var query = (from t in data
                group t by new {t.TimeStamp.Year}
                into grp
                select new
                    {
                        grp.Key.Year,
                        Count = grp.Count()
                    }).ToList();

    //let's instantiate the DataTable.
    var dt = new Google.DataTable.Net.Wrapper.DataTable();
    dt.AddColumn(new Column(ColumnType.String, "Year", "Year"));
    dt.AddColumn(new Column(ColumnType.Number, "Count", "Count"));

    foreach (var item in query)
    {
        Row r = dt.NewRow();
        r.AddCellRange(new Cell[]
        {
            new Cell(item.Year),
            new Cell(item.Count)
        });
        dt.AddRow(r);
    }

//Let's create a Json string as expected by the Google Charts API.
return dt.GetJson();
}

将生成以下 JSON 输出

{
    "cols": [
            {"type": "string", "id": "Year",  "label": "Year"}, 
            {"type": "number", "id": "Count", "label": "Count"}
        ], 
    "rows": [
            {"c": [{"v": "2011"}, {"v": "1860"}]}, 
            {"c": [{"v": "2012"}, {"v": "2000"}]}
        ]
}

,这可以在 Asp.NET WebAPI 中使用,也可以直接在 ASP.NET MVC 控制器中使用。

Another way to achieve this is to use the Google DataTable .Net Wrapper (https://googledatatablelib.codeplex.com/) which gives a possibility to work with a strongly typed System.DataTable that can then be converted into the google.datatable visualization JSON format.

This server side code

public string GetStatisticsForChart(string messageCode)
{
    //some repository that returns data....
    var data = _statisticsRepository.GetPerMessage(messageCode);

    //It simply returns a list of objects with Year and Count properties.
    var query = (from t in data
                group t by new {t.TimeStamp.Year}
                into grp
                select new
                    {
                        grp.Key.Year,
                        Count = grp.Count()
                    }).ToList();

    //let's instantiate the DataTable.
    var dt = new Google.DataTable.Net.Wrapper.DataTable();
    dt.AddColumn(new Column(ColumnType.String, "Year", "Year"));
    dt.AddColumn(new Column(ColumnType.Number, "Count", "Count"));

    foreach (var item in query)
    {
        Row r = dt.NewRow();
        r.AddCellRange(new Cell[]
        {
            new Cell(item.Year),
            new Cell(item.Count)
        });
        dt.AddRow(r);
    }

//Let's create a Json string as expected by the Google Charts API.
return dt.GetJson();
}

would generate the following JSON output

{
    "cols": [
            {"type": "string", "id": "Year",  "label": "Year"}, 
            {"type": "number", "id": "Count", "label": "Count"}
        ], 
    "rows": [
            {"c": [{"v": "2011"}, {"v": "1860"}]}, 
            {"c": [{"v": "2012"}, {"v": "2000"}]}
        ]
}

and this can be used either in Asp.NET WebAPI or directly in the ASP.NET MVC controller.

泼猴你往哪里跑 2024-08-30 15:51:25

上面示例中的预期 JSON 不是 JSON,而是 Javascript 对象文字。 JSON 只是 Javascript 对象文字表示法的子集,但如果 Google DataTable 使用类似的 JSON 进行初始化,上面的示例应该(并且确实)也可以工作。 (要获得正确的 JSON,只需在键周围加上双引号。)

因此,实际上您可以使用 DataContractJsonSerializerJavaScriptSerializer 为 Google DataTable 构建 JSON。但是,如果您的起点是 System.Data.DataTable,那么使用上面答案中提到的库可能会更容易。

The expected JSON in the example above is not JSON but a Javascript object literal. JSON is only a subset of Javascript object literal notation, but the example above should (and does) also work, if the Google DataTable is initialized with a similar looking JSON. (To get proper JSON just put double quotes around the keys.)

So actually you can use the DataContractJsonSerializer or JavaScriptSerializer to construct JSON for the Google DataTable. However, if your starting point is a System.Data.DataTable it is probably easier to use the library mentioned in the answer above.

揽清风入怀 2024-08-30 15:51:25

通过 C# 创建 google 图表数据表的最佳方法是迭代数据并填充模型类并以 JSON 形式返回填充模型,这里是模型表示:-

 public class GChartsDataTbl
{
    public List<Col> cols { get; set; }
    public List<Row> rows { get; set; }
}
public class Col
{
    public string id { get; set; }
    public string type { get; set; }
}

public class C
{
    public string v { get; set; }
    public object f { get; set; }
}

public class Row
{
    public List<C> c { get; set; }
}

The best way to create google charts datatable via c# is to iterate data and fill model class and return filled model as JSON , here is the model representation :-

 public class GChartsDataTbl
{
    public List<Col> cols { get; set; }
    public List<Row> rows { get; set; }
}
public class Col
{
    public string id { get; set; }
    public string type { get; set; }
}

public class C
{
    public string v { get; set; }
    public object f { get; set; }
}

public class Row
{
    public List<C> c { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文