java对象转换成json
2024-08-21 20:12:33
package com.bjs.acrosstime.utils; import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map; import org.apache.log4j.Logger;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory; /**
* @author Peter
*/
public class JsonUtils { private static Logger errLogger = Logger.getLogger("error");
private static Logger apiAccessLogger = Logger.getLogger("access");
private static final String PREFIX = "apiAccessAop"; //each thread has its own ObjectMapper instance
private static ThreadLocal<ObjectMapper> objMapperLocal = new ThreadLocal<ObjectMapper>(){
@Override
public ObjectMapper initialValue(){
return new ObjectMapper();
}
}; public static String toJSON(Object value) {
String result = null;
try {
result = objMapperLocal.get().writeValueAsString(value);
} catch (Exception e) {
e.printStackTrace();
}
// Fix null string
if ("null".equals(result)) {
result = null;
}
return result;
} public static <T> T toT(String jsonString, Class<T> clazz) {
try {
return objMapperLocal.get().readValue(jsonString, clazz);
} catch (Exception e) {
errLogger.error("toT error: "+ jsonString,e);
}
return null;
} public static <T> List<T> toTList(String jsonString, Class<T> clazz) {
try {
return objMapperLocal.get().readValue(jsonString, TypeFactory.collectionType(List.class, clazz));
} catch (Exception e) {
errLogger.error("toTList error: "+ jsonString,e);
}
return null;
} @SuppressWarnings("unchecked")
public static Map<String, Object> toMap(String jsonString) {
return toT(jsonString, Map.class);
} public static void main(String[] args) {
Message msg1 = new Message();
msg1.uid = "1";
msg1.opr_time = new Date();
msg1.content = "hello world---1"; Message msg2 = new Message();
msg2.uid = "2";
msg2.opr_time = new Date();
msg2.content = "hello world---2"; List<Message> list = new ArrayList<Message>();
list.add(msg1);
list.add(msg2);
final String json = toJSON(list);
System.out.println(json); //String l = "[{\"opr_time\":\"2012-05-12 12:33:22\",\"uid\":\"akun\",\"content\":\"\u5927\u5730\u9707\u7684\u4eba\u4eec\u5b89\u606f\u5427\"},{\"opr_time\":\"2012-05-12 12:33:25\",\"uid\":\"requelqi\",\"content\":\"\u6211\u56de\u5bb6\u4e86\"},{\"opr_time\":\"2012-05-12 12:37:25\",\"uid\":\"stone\",\"content\":\"\u4eca\u5929\u4e0d\u65b9\u4fbf\u6e38\u620f\"}]";
final List<Message> newMsg = JsonUtils.toTList(json, Message.class);
System.out.println(newMsg);
System.out.println((newMsg.get(0).uid));
} static class Message {
String uid;
Date opr_time;
@JsonIgnore
String content;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public Date getOpr_time() {
return opr_time;
}
public void setOpr_time(Date opr_time) {
this.opr_time = opr_time;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
} }
最新文章
- MySQL LIST分区
- JAVA String,StringBuffer与StringBuilder的区别??
- nmon工具的安装及简单使用
- OSD磁盘日常监控
- 0518 Scrum 项目 5.0
- C#记录对象的变化
- IDS IPS WAF之安全剖析
- 自由软件VS开源软件
- [HDOJ2874]Connections between cities(LCA, 离线tarjan)
- 设置程序图标-初识IOS
- cf B. Color the Fence
- net MVC 的八个扩展点
- deeplearning.ai 改善深层神经网络 week3 超参数调试、Batch正则化和程序框架 听课笔记
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
- R语言学习——图形初阶之折线图与图形参数控制
- C# winform TreeView中关于checkbox选择的完美类[转]
- Hadoop之MapReduce思维导图
- Robot - 1. robot framework环境搭建
- IDEA如何自动生成testNG的测试报告?
- html-day04