使用 google gson 转换Timestamp为JSON字符串
package com.test.base; import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp> { private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override
public Timestamp deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (!(jsonElement instanceof JsonPrimitive)) {
throw new JsonParseException("The data should be a string value");
}
try {
Date date = format.parse(jsonElement.getAsString());
return new Timestamp(date.getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
} @Override
public JsonElement serialize(Timestamp timestamp, Type type, JsonSerializationContext jsonSerializationContext) {
String dataFormatAsString = format.format(new Date(timestamp.getTime()));
return new JsonPrimitive(dataFormatAsString);
} }
@Test
public void gsonTest() {
Gson gson1 = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
CascadeReport tem = new CascadeReport();
tem.setDate(new Timestamp(new Date().getTime()));
tem.setDepartment("武汉刑侦");
String jsonString = gson1.toJson(tem, CascadeReport.class);
System.out.println(jsonString);
//////////////////////////////////////////////////////////
String reportData = "[{date:\"2016-01-01 09:00:01\",department:\"xxxx\",ipAddress:\"192.168.120.120\",failedNum:2,ruleIDs:\"1002,1003\",regionCode:168430083,account:\"李四\",type:1 }]";
List<CascadeReport> list = gson.fromJson(reportData, new TypeToken<List<CascadeReport>>() {
}.getType());
System.out.println(list.get(0).getDate().toGMTString());
}
使用 google gson 转换Timestamp为JSON字符串的更多相关文章
- [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.
创建类型适配类: import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; impo ...
- 使用 google gson 转换Timestamp或Date类型为JSON字符串.
http://blog.csdn.net/z69183787/article/details/13016289 创建类型适配类: import java.lang.reflect.Type; impo ...
- gson转换对象为json字符串时对特殊字符编码的问题
使用google的gson进行object和json的转换,如下: public static String object2json(Object obj) { Gson gson = new Gso ...
- 使用Google的Gson实现对象和json字符串之间的转换
使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...
- JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
- Java怎样高速构造JSON字符串
目标:依据key/value高速构造一个JSON字符串作为參数提交到web REST API服务上. 分别測试里阿里巴巴的FastJson和Google Gson,终于我採用了Google Gson来 ...
- gson和fastjson将json对象转换成javaBean 简单对照
今天在网上看代码时,发现项目使用了Gson,用于将json字符串转换成javaBean. 以前没使用过Gson,随即,简单入了个们, 想起fastjson也有将json字符串转换成javaBean的A ...
- 【Android进阶】Gson解析json字符串的简单应用
在客户端与服务器之间进行数据传输,一般采用两种数据格式,一种是xml,一种是json.这两种数据交换形式各有千秋,比如使用json数据格式,数据量会比较小,传输速度快,放便解析,而采用xml数据格式, ...
- JAVA中使用alibaba fastjson实现JSONObject、Object、Json字符串的转换
JAVA中使用alibaba fastjson实现JSONObject.Object.Json字符串的转换 Object转JSON字符串:String jsonStr = JSONObject.toJ ...
随机推荐
- Makedown常用符号整理
整理自:http://www.jianshu.com/p/1e402922ee32 不过这里发现博客园的makedown语法支持还不完善,代码语法显示挺有问题的,比较遗憾. 标题 # 一级标题## 二 ...
- silverlight 控件自定义样式 实现方法
1:在app.xaml中加入需实现的样式,如: <Application.Resources> <Style x:Key="NodeStyle" TargetTy ...
- vi 常用技巧
vi/vim : Visual Interface/VI iMproved VI命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,但是因为它的命令集众多,很多人都不习惯使用它,其实您只需 ...
- SQL Server视图
想来想去,总想写写SQL Server方面的知识,像视图.存储过程,大数据量操作的优化等等. 先把基础的知识总结个遍先,然后再寻求更高更远的发展.这篇文章,将带大家来看看视图. 何谓视图,视图包含行和 ...
- pos机套现是怎么回事
POS机是商家为了促进消费,向银行申请的刷卡机它的主要功能是转账就是通过客户的刷卡,把相对的金额转入商户的帐户银行会根据笔数或金额向商户收取手续费非法套现就是客户并未和商户产生贸易往来,单纯通过pos ...
- 【BZOJ】【2982】Combination
排列组合 Lucas定理模板题…… 感觉我做题顺序有点问题啊……应该是BZOJ 2982-->HDOJ 3037-->BZOJ 1272 好吧这个现在来看就有些水了…… 预处理一下fact ...
- 【HDOJ】【3037】Saving Beans
排列组合 啊……这题是要求c(n-1,0)+c(n,1)+c(n+1,2)+......+c(n+m-1,m) 这个玩意……其实就等于c(n+m,m) 好吧然后就是模P……Lucas大法好= = 我S ...
- AngularJS 基础
1. AngularJs 是一个JS 框架,是一种基于MVC的设计模式 2. script 需引用 <script src="angular.min.js">,安装包 ...
- GetSurfaceLevel
if( SUCCEEDED( g_pTexture->GetSurfaceLevel( 0, &pSurface) ) ) { pd3dDevice->Se ...
- 0327定时执行--存储过程--dbms_job--dbms_scheduler.create_job
--oracle job 定时执行 存储过程 --建一张测试表 create table Person( name ), sex ) ); / --创建测试的存储过程 create or replac ...