1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package com.hb.proj.utils;
- import java.util.List;
- import java.util.Map;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.ObjectMapper;
- public class JacksonUtils {
- //private static final Logger logger = LoggerFactory.getLogger(JacksonUtils.class);
-
- private final static ObjectMapper mapper = new ObjectMapper();
-
- public static String getJSON(Object obj){
- try {
- return mapper.writeValueAsString(obj);
- }
- catch(Exception e) {
- throw new RuntimeException("jackson序列化对象时出错");
- }
- }
- public static <T> List<T> getList(String json,Class<T> cls){
- try {
- return mapper.readValue(json, new TypeReference<List<T>>() {});
- }
- catch(Exception e) {
- throw new RuntimeException("jackson参数转换出错");
- }
-
- }
-
- public static List<Map<String,Object>> getMaps(String json){
- try {
- return mapper.readValue(json, new TypeReference<List<Map<String,Object>>>() {});
- }
- catch(Exception e) {
- throw new RuntimeException("jackson参数转换出错");
- }
-
- }
-
- @SuppressWarnings("unchecked")
- public static Map<String,Object> getMap(String json){
- try {
- return mapper.readValue(json, Map.class);
- }
- catch(Exception e) {
- throw new RuntimeException("jackson参数转换出错");
- }
- }
-
- public static <T> T get(String json,Class<T> cls) {
- try {
- return mapper.readValue(json, cls);
- }
- catch(Exception e) {
- throw new RuntimeException("jackson参数转换出错");
- }
- }
- }
|