如何将日期值从 JSON 返回到 Google Visualization API

发布于 2024-09-18 04:04:26 字数 1094 浏览 4 评论 0原文

有没有办法在 Google Visualization API 中从 JSON 检索日期值? 以下是playground的代码段请将以下代码复制到其中

当您运行代码时,您不会得到任何结果。您应该从我用注释标记的日期值中删除引号才能检索结果。

function drawVisualization() {
var JSONObject = {
cols:
    [
        {id: 'header1', label: 'Header1', type: 'string'},
        {id: 'header2', label: 'Header2', type: 'date'}
    ],
rows: 
    [
        {
            c:
                [
                    {v: 'Value1'}, 
                    {v: "new Date(2010, 3, 28)"}  // <= This is the format I receive from WebService
                ]
        },
        {
            c:
                [
                    {v: 'Value2'},
                    {v: new Date(2010, 3, 28)} // <=This is the format Google API accepts
                ]
        }
    ]
};

var data = new google.visualization.DataTable(JSONObject, 0.5);

visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, {'allowHtml': true});
}

is there a way to retrieve date value from JSON in Google Visualization API?
Here is the snipplet for playground please copy the code below into it

When you run the code you won't have anything in result. you should remove the quotes from the date value I marked with comment in order to retrieve result.

function drawVisualization() {
var JSONObject = {
cols:
    [
        {id: 'header1', label: 'Header1', type: 'string'},
        {id: 'header2', label: 'Header2', type: 'date'}
    ],
rows: 
    [
        {
            c:
                [
                    {v: 'Value1'}, 
                    {v: "new Date(2010, 3, 28)"}  // <= This is the format I receive from WebService
                ]
        },
        {
            c:
                [
                    {v: 'Value2'},
                    {v: new Date(2010, 3, 28)} // <=This is the format Google API accepts
                ]
        }
    ]
};

var data = new google.visualization.DataTable(JSONObject, 0.5);

visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, {'allowHtml': true});
}

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

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

发布评论

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

评论(3

小糖芽 2024-09-25 04:04:26

我自己刚刚遇到这个问题,所以我想我应该粘贴 google api 文档中的答案,位于此处 http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable

“JSON 不支持 JavaScript 日期值(例如,“new Date(2008,1,28,0,31,26)";API 实现确实如此。但是,API 现在支持将日期作为字符串的自定义有效 JSON 表示形式,格式如下:Date(year,month,day [,小时,分钟,秒[,毫秒]]) 其中一天之后的所有内容都是可选的, 月份是从零开始的。”

I just ran into this problem myself, so I thought I'd paste the answer from the google api documentation, located here http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable

"JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based."

沙沙粒小 2024-09-25 04:04:26

我遇到了同样的问题,上述解决方案不起作用。经过几个小时的搜索后,我发现以下帖子和其中的解决方案有效。

https://groups.google.com/forum/# !msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ

不要在 json 字符串中包含“new”,因此它只是:
v:"日期(2009, 9, 28)"

I was running into same problem and above solution did not work. After searching for hours I found following post and the solution in there worked.

https://groups.google.com/forum/#!msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ

Do not include "new" in the json string so it will be just:
v:"Date(2009, 9, 28)"

泼猴你往哪里跑 2024-09-25 04:04:26

我认为引用不在代码段中的正确位置 "new Date(2010, 3, 28")
改写 "new Date(2010, 3, 28)"

Json 格式不接受 javascript 对象,因此服务器返回一个字符串。
JSON 只知道数字、布尔常量、字符串、null、向量和“对象”(更多的是字典)。

我想您必须对返回的字符串执行 eval() (不要忘记检查输入)。

另一种选择是使用正则表达式来提取字段,例如 /new Date\((\d+),(\d+),(\d+)\)/

I suppose that the quote is not at the correct place in your snippet "new Date(2010, 3, 28")
Write instead "new Date(2010, 3, 28)"

Json format does not accept javascript object so the server return a string.
JSON knows only numbers, boolean constants, string, null, vector and 'object' (much more a dictionnary).

I suppose that you have to perform an eval() of the returned string (do not forget to check inputs).

Another alternative is to use a Regex to extract the fields something like /new Date\((\d+),(\d+),(\d+)\)/ will work.

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