ObjectMapper 将其他对象转换为json
来源:互联网 发布:炎锤网络 编辑:程序博客网 时间:2024/06/03 09:27
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URL;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
*<p>
* <ol style='font-size:12px'>
* <li>
* Describe :
* </li>
* <li>Write date :2011-3-31</li>
* <li>
* Developer :<a href='MailTo:abc-127@qq.com' title='Send to the E-mail'>English name is Fd wang</a>
* </li>
* <li>Company : </li>
* </ol>
*</p>
*/
public class JsonFormater {
private static ObjectMapper mapper=new ObjectMapper();
private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.setDateFormat(format);
}
public static String getJsonValue(Object object) {
StringWriter writer=null;
try {
writer=new StringWriter();
mapper.writeValue(writer,object);
writer.flush();
return writer.toString();
} catch (Exception e) {
throw new RuntimeException("JSON 格式化出错",e);
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static <T> T toObject(String url,Class<T> clazz){
try {
return mapper.readValue(url, clazz);
} catch (JsonParseException e) {
throw new RuntimeException("JSON 格式化出错",e);
} catch (JsonMappingException e) {
throw new RuntimeException("JSON 格式化出错",e);
} catch (IOException e) {
throw new RuntimeException("网络连接异常",e);
}
}
public static String getJsonValue(ObjectMapper objectMapper,Object object) {
StringWriter writer=null;
try {
writer=new StringWriter();
if(objectMapper==null){
mapper.writeValue(writer, object);
}else{
objectMapper.writeValue(writer, object);
}
writer.flush();
return writer.toString();
}catch (Exception e) {
throw new RuntimeException("JSON 格式化出错",e);
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean writeJsonValue(ObjectMapper objectMapper,OutputStream os,Object object) {
boolean bool=true;
try {
if(objectMapper==null){
mapper.writeValue(os, object);
}else{
objectMapper.writeValue(os, object);
}
os.flush();
} catch (Exception e) {
throw new RuntimeException("JSON 格式化出错",e);
}
return bool;
}
public static boolean writeJsonValue(OutputStream os,Object object) {
boolean bool=true;
try {
mapper.writeValue(os, object);
os.flush();
} catch (Exception e) {
throw new RuntimeException("JSON 格式化出错",e);
}
return bool;
}
public static boolean writeJsonValue(HttpServletResponse response,Object object) {
return writeJsonValue(null,response, object);
}
public static boolean writeJsonValue(ObjectMapper objectMapper,HttpServletResponse response,Object object) {
response.setContentType("text/html;charset=UTF-8");
boolean bool=true;
if(objectMapper==null){
objectMapper=mapper;
}
try {
objectMapper.writeValue(response.getOutputStream(), object);
} catch (Exception e) {
throw new RuntimeException("JSON 格式化出错",e);
}
return bool;
}
public static <T> T readValueFromURL(String url,Class<T> clasz){
try {
return mapper.readValue(new URL(url), clasz);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static <T> T readValueFromStream(InputStream in,TypeReference<T> valueTypeRef){
try {
return (T)mapper.readValue(in, valueTypeRef);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
0 0
- ObjectMapper 将其他对象转换为json
- 使用 jackson ObjectMapper将java对象转换为json对象
- 将其他对象转换为JSON对象
- ObjectMapper对象(json格式转换)
- 将对象转换为json对象
- 使用ObjectMapper把json转换为实体-时间转换问题
- ObjectMapper将json字符串转换成泛型List/map
- 将.Net对象转换为Json字符串
- 将java对象转换为Json
- json2html:将JSON对象转换为HTML
- JS 将JSON对象转换为字符串
- 将json对象转换为字符串
- 将JSON字符串转换为String对象
- 将json转换为java集合对象
- android将对象转换为json格式
- 将对象转换为json字符串
- 如何将bean转换为json对象
- JS 将JSON对象转换为字符串
- 常用的Hadoop Common组件属性分享
- linux编程中的socket编程--server端ip0.0.0.0问题
- 微信营销的差异化,你找到了吗?
- GPU上大规模稀疏矩阵特征值计算高效算法之二——稀疏矩阵
- Haproxy源码分析一、概述
- ObjectMapper 将其他对象转换为json
- Java web开发一个帐号同一时间只能一个人登录
- C和C++中const的区别
- LeetCode | Decode Ways(译码方式)
- mysql用户操作
- GPU编程之CUDA(三)——关于联想Y470
- 利用ajaxfileupload.js实现跨域上传图片并处理返回的数据
- 使用MariaDB集群运维可能涉及的细节之binarylog
- 建有序链表,还是要打好基础的。。。