java json u0026_使用Gson将对象类转成Json对象时出现\u003d的问题

该博客介绍了如何在Java中使用Gson库处理Json对象,特别是解决在转换过程中遇到的特殊字符如u003d的问题。通过自定义序列化和反序列化器,实现了日期格式的转换,并提供了处理Json字符串到对象的反序列化方法。
摘要由CSDN通过智能技术生成

package com.xinwei.util;

import java.lang.reflect.Type;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

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;

import com.google.gson.reflect.TypeToken;

public class JsonUtil{

// 支持转换的日期格式

public static final DateFormat[] ACCEPT_DATE_FORMATS = {

new SimpleDateFormat("MM/dd/yyyy"),

new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"),

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),

new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")

};

/**

* 将对象序列化为json字符串,只对有@expose注解的进行序列化

* @param obj

* @return

*/

public static String toJsonWithExclusive(Object obj){

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()

.registerTypeAdapter(Date.class, new MyDateSerializer())

.create();

String json = gson.toJson(obj);

return json;

}

/**

* 将对象序列化为json字符串,不区分注解

* @param obj

* @return

*/

public static String toJson(Object obj){

Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new MyDateSerializer())

.create();

String json = gson.toJson(obj);

return json;

}

public static String toJsonFilterEquals(Object obj){

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

String json = gson.toJson(obj);

return json;

}

/**

* 将对象序列化为json字符串,不区分注解

* @param obj

* @return

*/

public static String toJson0(Object obj){

String json = toJson(obj, Date.class, new MyTimestampSerializer());

return json;

}

/**

* 将对象序列化为json字符串,不区分注解

* @param obj

* @return

*/

@SuppressWarnings("rawtypes")

public static String toJson(Object obj,Class clazz,JsonSerializer serializer){

Gson gson = new GsonBuilder().registerTypeAdapter(clazz, serializer)

.create();

String json = gson.toJson(obj);

return json;

}

/**

* 将json字符串反序列化为对象,不区分注解

* @param jsonStr

* @param classOfT

* @return

*/

@SuppressWarnings("unchecked")

public static T fromJson(String jsonStr,Type type){

Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new MyDateDeserializer())

.create();

return (T)gson.fromJson(jsonStr, type);

}

/**

* 将json字符串反序列化为对象,区分注解

* @param jsonStr

* @param classOfT

* @return

*/

@SuppressWarnings("unchecked")

public static T fromJsonWithExclusive(String jsonStr,Type type){

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()

.registerTypeAdapter(Date.class, new MyDateDeserializer())

.create();

return (T)gson.fromJson(jsonStr, type);

}

@SuppressWarnings("rawtypes")

public static Map fromJson(String jsonStr){

Type type = new TypeToken(){}.getType();

Gson gson = new Gson();

return gson.fromJson(jsonStr, type);

}

/**

* date类型反序列化器

* @author jiweibin

*

*/

private static class MyDateDeserializer implements JsonDeserializer{

public Date deserialize(JsonElement json, Type typeOfT,

JsonDeserializationContext context) throws JsonParseException {

// 遍历日期支持格式,进行转换

for (DateFormat format : ACCEPT_DATE_FORMATS) {

try {

return format.parse(json.getAsString());

} catch (Exception e) {

continue;

}

}

return null;

/* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

try {

return sdf.parse(json.getAsString());

} catch (ParseException e) {

e.printStackTrace();

}

return null;*/

}

}

/**

* timestamp类型反序列化器

* @author jiweibin

*

*/

private static class MyTimestampSerializer implements JsonSerializer{

public JsonElement serialize(Date src, Type typeOfSrc,

JsonSerializationContext context) {

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

return new JsonPrimitive(sdf.format(src));

}

}

/**

* date类型序列化器

* @author jiweibin

*

*/

private static class MyDateSerializer implements JsonSerializer{

public JsonElement serialize(Date src, Type typeOfSrc,

JsonSerializationContext context) {

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

return new JsonPrimitive(sdf.format(src));

}

}

public static void main(String[] args){

/*String json = "{'name': 'helloworlda','array':[{'a':'111','b':'222','c':'333'},{'a':'999'}],'address':'111','people':{'name':'happ','sex':'girl'}}";

JsonUtil.fromJson(json, JsonUtil.class);*/

Date date = new Date();

String dateStr = JsonUtil.toJson(date);

System.out.println(dateStr);

}

}

{"phoneNumber":"567768","deviceId":"08277599","deviceName":"PP399","alarmId":"201711160408","smcDesc":"您有一条工单未处理,请及时处理alarmId:201711160408","dataId":"2017112000060000489","detailUrl":"http://localhost:8080/workorderms/views/orderms/workorderDetail.html?from\u003dfinish.html\u0026state\u003d\u0026groupId\u003d201711160408\u0026procInstId\u003d30472\u0026taskId\u003d30512"}

{"phoneNumber":"45645656","deviceId":"08277588","deviceName":"PP388","alarmId":"201711160405","smcDesc":"您有一条工单未处理,请及时处理alarmId:201711160405","dataId":"2017112000130000496","detailUrl":"http://localhost:8080/workorderms/views/orderms/workorderDetail.html?from=finish.html&state=&groupId=201711160405&procInstId=30450&taskId=30748"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值