java json 工具类_JsonUtil工具类

使用的是fastJson

package util;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

public enum LetterType {

UPPERCASE(1),LOWERCASE(2);

private final int type;

private LetterType(int type) {

this.type = type;

}

public int getType() {

return type;

}

}

/**

* json字符串转换javabean

* @param json json字符串

* @param bean 对象.class

* @return T

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

* */

@SuppressWarnings("unchecked")

public static T jsonStrToJava(String json,Class> bean) throws JsonParseException, JsonMappingException, IOException{

ObjectMapper objectMapper = new ObjectMapper();

if(json.indexOf("/Date(")>0) {

json = JsonUtil.jsonDateToJavaDate(json);

}

return (T) objectMapper.readValue(json, bean);

}

/**

* json字符串转换javabean

* @param json json字符串

* @param bean 对象.class

* @param toggleCase 是否转换key的大小写

* @param letterType 转换枚举类 UPPERCASE-大写、LOWERCASE-小写

* @return T

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

* */

@SuppressWarnings("unchecked")

public static T jsonStrToJava(String json,Class> bean,boolean toggleCase,LetterType letterType) throws JsonParseException, JsonMappingException, IOException{

ObjectMapper objectMapper = new ObjectMapper();

if(toggleCase) {

json =JsonUtil.convertLetter(json,letterType);

}

if(json.indexOf("/Date(")>0) {

json = JsonUtil.jsonDateToJavaDate(json);

}

return (T) objectMapper.readValue(json, bean);

}

/**

* json字符串转换key的大小写

* @param json json字符串

* @param letterType 转换枚举类 UPPERCASE-大写、LOWERCASE-小写

* @return json字符串

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

* */

public static String convertLetter(String json,LetterType letterType) throws JsonParseException, JsonMappingException, IOException {

Map map = JsonUtil.jsonStrToJava(json, Map.class);

for (Map.Entry entry : map.entrySet()) {

if(letterType.getType()==1) {

map.put(entry.getKey().toUpperCase(), entry.getValue());

}else {

map.put(entry.getKey().toLowerCase(), entry.getValue());

}

map.remove(entry.getKey());

}

return JsonUtil.javaToJson(map);

}

/**

*  javabean转换json字符串

* @return

* @throws JsonProcessingException

* */

public static String javaToJson(Object bean) throws JsonProcessingException {

ObjectMapper objectMapper = new ObjectMapper();

String json = objectMapper.writeValueAsString(bean);

return json;

}

/**

*  json字符串中存在Date(1558427619277+0800)类似的时间格式进行转换

*  @param    json json字符串

* @return     json json字符串

* @throws JsonProcessingException

* */

public static String jsonDateToJavaDate(String json) throws JsonParseException, JsonMappingException, IOException {

Map map = JsonUtil.jsonStrToJava(json, Map.class);

for (Map.Entry entry : map.entrySet()) {

if(entry.getValue().indexOf("/Date(")>0) {

String dateJSON = entry.getValue().replace("/Date(","").replace(")/","");

Long dateLong = Long.valueOf(dateJSON.substring(0, dateJSON.length()-5));

Date date = new Date(dateLong);

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

map.put(entry.getKey(), format.format(date));

}

}

return JsonUtil.javaToJson(map);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值