JsonUtil.java 1.4 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
package com.lyms.talkonlineweb.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;


/**
* Created by Administrator on 2019-09-06.
*/
public class JsonUtil {



/**
* 对象转换为String
* @param cls
* @return
*/
public static String obj2Str(
Object cls) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(DateUtil.getDateFormat(DateUtil.YYYY_MM_DD_HH_MM_SS));
return objectMapper.writeValueAsString(cls);
} catch (Exception e) {

}
return null;
}

/**
* json转换为map
* @param json
* @param hashMapClass
* @return
*/
public static Map<String, Object> str2Map(String json, Class<HashMap> hashMapClass) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json,hashMapClass);
} catch (Exception e) {

}
return null;
}

/**
* 接送转换为对象
* @param json
* @param clzz
* @param <T>
* @return
*/
public static <T> T str2Obj(String json, Class<T> clzz) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json,clzz);
} catch (Exception e) {

}
return null;
}
}