import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static final Logger log = Logger.getLogger(JsonUtils.class);
private final static ObjectMapper objectMapper = new ObjectMapper();
/**
* get Instance of ObjectMapper
*
* @return ObjectMapper
*/
public static ObjectMapper getInstance() {
return objectMapper;
}
/**
* JavaBean convert to Json
* @param bean
* @return
*/
public static String bean2Json(Object bean) {
try {
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
return objectMapper.writeValueAsString(bean);
} catch (JsonProcessingException e) {
log.error("jackson进程异常:" + e.getMessage());
return null;
}
}
/**
* Json convert to JavaBean
* @param json
* @param clazz
* @return
*/
public static <T> T json2Bean(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (JsonParseException e) {
log.error("Jackson解析异常:" + e.getMessage());
return null;
} catch (JsonMappingException e) {
log.error("Jackson映射异常:" + e.getMessage());
return null;
} catch (IOException e) {
log.error("IO异常:" + e.getMessage());
return null;
}
}
/**
* Json convert to Map
* @param json
* @return
*/
@SuppressWarnings("unchecked")
public static <T> Map<String, Object> json2Map(String json) {
try {
return objectMapper.readValue(json, Map.class);
} catch (JsonParseException e) {
log.error("Jackson解析异常:" + e.getMessage());
return null;
} catch (JsonMappingException e) {
log.error("Jackson映射异常:" + e.getMessage());
return null;
} catch (IOException e) {
log.error("IO异常:" + e.getMessage());
return null;
}
}
/**
* Json convert to Map with javaBean
* @param json
* @param clazz
* @return
*/
public static <T> Map<String, T> json2Map(String json, Class<T> clazz) {
Map<String, Map<String, Object>> map;
try {
map = objectMapper.readValue(json, new TypeReference<Map<String, T>>(){});
Map<String, T> result = new HashMap<String, T>();
for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
result.put(entry.getKey(), map2Bean(entry.getValue(), clazz));
}
return result;
} catch (JsonParseException e) {
log.error("Jackson解析异常:" + e.getMessage());
return null;
} catch (JsonMappingException e) {
log.error("Jackson映射异常:" + e.getMessage());
return null;
} catch (IOException e) {
log.error("IO异常:" + e.getMessage());
return null;
}
}
/**
* Json convert to List with JavaBean
* @param json
* @param clazz
* @return
*/
public static <T> List<T> json2List(String json, Class<T> clazz) {
List<Map<String, Object>> list;
try {
list = objectMapper.readValue(json, new TypeReference<List<T>>() {});
List<T> result = new ArrayList<T>();
for (Map<String, Object> map : list) {
result.add(map2Bean(map, clazz));
}
return result;
} catch (JsonParseException e) {
log.error("Jackson解析异常:" + e.getMessage());
return null;
} catch (JsonMappingException e) {
log.error("Jackson映射异常:" + e.getMessage());
return null;
} catch (IOException e) {
log.error("IO异常:" + e.getMessage());
return null;
}
}
/**
* Map convert to JavaBean
*
* @param map
* @param clazz
* @return bean
*/
@SuppressWarnings("rawtypes")
public static <T> T map2Bean(Map map, Class<T> clazz) {
return objectMapper.convertValue(map, clazz);
}
}
分享到:
相关推荐
JsonUtil json工具类 JsonUtil json工具类
JSON jar包含源码及应用类JSONUtil,可以直接查看源码不用反编译,并且可以直接调用JSONUtil工具类进行,JSon字符串转换为JavaBean ,并可以把JavaBean转换为JSon字符串。
JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码JSONUtil最新源代码...
JsonUtil字符串和对象间转换工具类
json工具类,基于com.google.code.gson封装的json转换工具类
Google json工具类,将java对象装换为json,支持泛型和任意类型
Json的一个2个工具类,用于Json字符串和Object、List、HashMap的转换。
自己封装的json数据转换类,并打成jar包 可以直接使用
leano-jsonutil LCMC(更少代码,更舒适) 简单的Json Util项目,可帮助您快速创建Json。 Json util是对json操作的开发人员友好的sepc和api。 GsonUtil instance = GsonUtil.instance(); //Even number of params...
json解析大全android,java
JSONUtil java工具类,快速方便读取JSON
json 工具类 转换 获取泛型的Collection Type JavaType javaType = mapper.getTypeFactory().constructParametricType( collectionClass, elementClasses);
包含了 通过string转json,通过jsonarray转json,获取json里面的值,包括,boolean,int,long,string,json,jsonarry等类型,同时还可以给json赋值,是一个比较多用途的工具类,同时还进行了异常处理
json_TO_clientdataset_JSONUtil包.rar 整理了一下 JSON 轉CLIENTDATASET 功能 相關單元文件, 從網絡下載的工具類: SUPEROBJECT.PAS, myUnicode.pas , uJSONDB.pas, uDeltaToSQL.pas, superxmlparser.pas 測試...
主要介绍了Java实现的JSONUtil工具类与用法,结合实例形式分析了Java操作json格式数据工具类JSONUtil的定义与简单使用技巧,需要的朋友可以参考下
java中将实体类转化成json对象的类 Java的JsonUtil.java java转化实体类为json的工具
主要介绍了springboot封装JsonUtil,CookieUtil工具类过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
commonUtils1. HttpUtil / AsyncHttpClient2. JsonUtil3. WebSocketClientUtil4. TcpClientUtil