使用ObjectMapper 序列化Json
来源:互联网 发布:微信广告植入系统源码 编辑:程序博客网 时间:2024/06/08 08:06
使用ObjectMapper 序列化Json
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json序列化工具类
*/
public class JsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
mapper.setDateFormat(new SimpleDateFormat("yyyy.MM.dd HH:mm"));
}
public static String object2Json(Object obj) throws JsonProcessingException {
return mapper.writeValueAsString(obj);
}
public static <T> T jsonStr2Object(String jsonStr, Class<T> clazz) throws IOException {
return mapper.readValue(jsonStr, clazz);
}
public static <T> T jsonNode2Object(JsonNode jsonNode, Class<T> clazz) throws JsonProcessingException {
return mapper.treeToValue(jsonNode, clazz);
}
public static JsonNode jsonStr2JsonNode(String jsonStr) throws IOException {
return mapper.readTree(jsonStr);
}
public static JsonNode Object2JsonNode(Object obj) {
return mapper.valueToTree(obj);
}
public static JsonNode inputStreamJsonNode(InputStream inputStream) throws JsonProcessingException, IOException {
return mapper.readTree(inputStream);
}
public static <T> T jsonStr2TypeReference(String jsonStr, TypeReference<T> valueTypeRef) throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(jsonStr, valueTypeRef);
}
}
使用ObjectMapper 序列化Json
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json序列化工具类
*/
public class JsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
mapper.setDateFormat(new SimpleDateFormat("yyyy.MM.dd HH:mm"));
}
public static String object2Json(Object obj) throws JsonProcessingException {
return mapper.writeValueAsString(obj);
}
public static <T> T jsonStr2Object(String jsonStr, Class<T> clazz) throws IOException {
return mapper.readValue(jsonStr, clazz);
}
public static <T> T jsonNode2Object(JsonNode jsonNode, Class<T> clazz) throws JsonProcessingException {
return mapper.treeToValue(jsonNode, clazz);
}
public static JsonNode jsonStr2JsonNode(String jsonStr) throws IOException {
return mapper.readTree(jsonStr);
}
public static JsonNode Object2JsonNode(Object obj) {
return mapper.valueToTree(obj);
}
public static JsonNode inputStreamJsonNode(InputStream inputStream) throws JsonProcessingException, IOException {
return mapper.readTree(inputStream);
}
public static <T> T jsonStr2TypeReference(String jsonStr, TypeReference<T> valueTypeRef) throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(jsonStr, valueTypeRef);
}
}
0 0
- 使用ObjectMapper 序列化Json
- 使用jackson的ObjectMapper json转object
- [Jackson] 使用ObjectMapper对含有任意key的JSON进行反序列化
- 使用ObjectMapper把json转换为实体-时间转换问题
- 使用 jackson ObjectMapper将java对象转换为json对象
- ObjectMapper JSON的 处理
- jackson ObjectMapper 序列化成json; 属性值为null时转换为""空字符串
- 多对一双向关联后使用org.codehaus.jackson.map.ObjectMapper转json时报org.codehaus.jackson.map.ObjectMapper;
- ObjectMapper对象(json格式转换)
- ObjectMapper 简单使用
- IOS网络操作-使用Alamofire和ObjectMapper处理JSON转对象
- json字符串出现转义符,导致调用者使用ObjectMapper解析成Map报错
- java 使用ObjectMapper的方式实现Json和bean的转换
- json和bean的转换工具Jackson ObjectMapper类使用解析
- ObjectMapper
- ObjectMapper
- ObjectMapper
- 使用JavaScriptSerializer进行JSON序列化
- 数据格式化函数
- 韩文歌曲音译
- 升级win10后 MarkdownPad 2 无法实时渲染HTML页面问题
- Java-动态传参调用kettle转换和作业
- spoj7258:Lexicographical Substring Search 后缀自动机
- 使用ObjectMapper 序列化Json
- 第4周项目1-求最大公约数
- 行测材料分析公式总结
- 线程与进程区别
- 蓝桥杯 第七届 C/C++ B 省赛 凑算式
- 第四周 项目二 太乐了
- “Unable to install breakpoint due to missing line number attributes,Modify compiler options togenera
- DRM in Android
- POJ2386 Lake Counting