带有解析时间或时间戳的 JSON 到 amCharts [英] JSON with parsed time or timestamp to amCharts

查看:38
本文介绍了带有解析时间或时间戳的 JSON 到 amCharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些关于 JSON 和日期解析的新手问题.

我的服务器有一个 JSON 响应,其中包含转换为日期/时间的时间戳,然后它被缓存.我认为直接在服务器上转换时间戳会利用客户端的脚本.所以我这样做了,我从服务器得到了这样的响应:

<预><代码>[{日期时间":2014/05/12 16:00",数据":172.0",数据2":192",数据3":172.0"},{...}]

这个 JSON 文件然后被提取到 amCharts,在 Chrome 中工作得很好,而在 Firefox 和 Safari 中不会像这样解析日期,我得到错误.

然后我将 JSON 响应从服务器更改为:

<预><代码>[{日期时间":1213214234,数据":172.0,数据2":192,数据3":172.0},{...}]

我如何使用 amCharts 处理时间戳,或者如何为第一个示例制定解决方法?因为我的 json 文件包含 2000 多个数据集,所以在服务器上转换时间戳是一种好习惯吗?

谢谢.

解决方案

AmCharts 可以将日期作为时间戳处理(不需要额外的操作),还可以使用几乎任何日期格式处理设置为字符串的日期.如果你的日期是字符串,你应该设置 chart.dataDateFormat = "YYYY/MM/DD JJ:NN";

在此处查看模式列表:http://www.amcharts.com/tutorials/格式化日期/

I have some newbie questions about JSON and dates parsing.

I have a JSON response from my server which contain converted timestamp to date/time, and then it's cached. I thought converting timestamp directly on server would leverage script on client side. So i did that and i had response from server like this:

[
  {"datetime": "2014/05/12 16:00", 
   "data": "172.0",
   "data2": "192",
   "data3": "172.0"
  },
  {...}
]

This JSON file is then fetched to amCharts, in Chrome works perfectly fine, while in Firefox and Safari won't parse date like that and i get errors.

Then i changed JSON response from server to this:

[
  {"datetime": 1213214234, 
   "data": 172.0,
   "data2": 192,
   "data3": 172.0      
  },
  {...}
]

How can i handle timestamp with amCharts or can make a workaround for first example? And is it good practice to convert timestamps on server, because my json file contain over 2000 data sets?

Thanks.

解决方案

AmCharts can handle dates as timestamps (no additional action is required), and also can handle dates set as strings using almost any date format. If your dates are strings, you should set chart.dataDateFormat = "YYYY/MM/DD JJ:NN";

Check the list of patterns here: http://www.amcharts.com/tutorials/formatting-dates/

这篇关于带有解析时间或时间戳的 JSON 到 amCharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆