主要介绍Gson用法
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
json数据
{
"msg": "true",
"result": [
{
"attr": "0.0",
"id": "100.0",
"product": "0",
"status": "297.086",
"updateTime": "20170103"
},
{
"attr": "0.0",
"id": "100.0",
"product": "0",
"status": "162.678"
}
],
"total": 60,
"success": true
}
对象
@Data
public static class RestResponse2 {
private Object msg;
private List<Product> result;
private Object total;
private Object success;
}
@Data
public static class Product {
private String id;
private String product;
private String unit;
private String attr;
private String status;
private Date updateTime;
}
1、复杂类型反序列化
根据key映射对象中的属性,封装对象
RestResponse2 obj = new GsonBuilder().create().fromJson(json, RestResponse2.class);
System.out.println(obj);
结果:
GsonTest.RestResponse2(msg=true, result=[GsonTest.Product(id=100.0, product=0, unit=null, attr=0.0, status=297.086, updateTime=Tue Jan 03 00:00:00 CST 2017), GsonTest.Product(id=100.0, product=0, unit=null, attr=0.0, status=162.678, updateTime=null)], total=60.0, success=true)
数组也会自动反序列化,时间也能解析,常用时间字符串格式能解析,最好指定时间格式化
2、存在时间的序列化与反序列化
@Test
public void gsonWithDate() {
String json = "{\"msg\":\"true\",\"result\":[{\"attr\":\"0.0\",\"id\":\"100.0\",\"product\":\"0\",\"status\":\"297.086\",\"updateTime\":\"20170103091145\"},{\"attr\":\"0.0\",\"id\":\"100.0\",\"product\":\"0\",\"status\":\"162.678\"}],\"total\":60,\"success\":true}";
Gson gson = new GsonBuilder().setDateFormat("yyyyMMddhhmmss").create();
RestResponse2 obj = gson.fromJson(json, RestResponse2.class);
System.out.println(obj);
//对象转json
String obj2Json = gson.toJson(obj);
System.out.println(obj2Json);
}
结果:时间正常解析
GsonTest.RestResponse2(msg=true, result=[GsonTest.Product(id=100.0, product=0, unit=null, attr=0.0, status=297.086, updateTime=Tue Jan 03 00:00:00 CST 2017), GsonTest.Product(id=100.0, product=0, unit=null, attr=0.0, status=162.678, updateTime=null)], total=60.0, success=true)
{"msg":"true","result":[{"id":"100.0","product":"0","attr":"0.0","status":"297.086","updateTime":"20170103"},{"id":"100.0","product":"0","attr":"0.0","status":"162.678"}],"total":60.0,"success":true}
备注:gson能自动处理一些常用格式时间字符串yyyyMMdd、yyyy-MM-dd、yyyy-MM-dd hh:mm:ss
3、将所有属性定义成Object类型,如果json中还有json数组会转成ArrayList,具体类型LinkedTreeMap
@Data
public static class RestResponse {
private Object msg;
private Object result;
private Object total;
private Object success;
}
@Test
public void gsonWithObjectArray() {
String json = "{\"msg\":\"true\",\"result\":[{\"attr\":\"0.0\",\"id\":\"100.0\",\"product\":\"0\",\"status\":\"297.086\",\"updateTime\":\"20170103\"},{\"attr\":\"0.0\",\"id\":\"100.0\",\"product\":\"0\",\"status\":\"162.678\"}],\"total\":60,\"success\":true}";
RestResponse obj = new GsonBuilder().create().fromJson(json, RestResponse.class);
System.out.println(obj);
}
内存中预览
或者对象中包含对象,不指定内部对象具体类型,LinkedTreeMap数据类型
@Test
public void gsonWithObjInnerObj() {
String json = "{\"msg\":\"true\",\"result\":{\"attr\":\"0.0\",\"id\":\"100.0\",\"product\":\"0\",\"status\":\"297.086\",\"updateTime\":\"20170103\"},\"total\":60,\"success\":true}";
RestResponse obj = new GsonBuilder().create().fromJson(json, RestResponse.class);
System.out.println(obj);
}

4、如果json串和对象的属性名不一致,可使用别名 如果对象和json 的key不一样,可用注解
json样例:{"biz_data": "test"}
@SerializedName("biz_data")
private BizData bizData;
这样可以反序列化转换到对象,同样是这个注解,序列化成json可得到:{"biz_data": "test"}
5、如果java对象不是规范驼峰命名
@Data
public static class RestResponse3 {
private String Name_OCR;
}
@Test
public void firstCharUp2json() {
RestResponse3 restResponse3 = new RestResponse3();
restResponse3.setName_OCR("test_ord");
String rst = new GsonBuilder().create().toJson(restResponse3);
System.out.println(rst);
}
序列化结果
{"Name_OCR":"test_ord"}