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;
}
} }

最新文章

  1. MySQL LIST分区
  2. JAVA String,StringBuffer与StringBuilder的区别??
  3. nmon工具的安装及简单使用
  4. OSD磁盘日常监控
  5. 0518 Scrum 项目 5.0
  6. C#记录对象的变化
  7. IDS IPS WAF之安全剖析
  8. 自由软件VS开源软件
  9. [HDOJ2874]Connections between cities(LCA, 离线tarjan)
  10. 设置程序图标-初识IOS
  11. cf B. Color the Fence
  12. net MVC 的八个扩展点
  13. deeplearning.ai 改善深层神经网络 week3 超参数调试、Batch正则化和程序框架 听课笔记
  14. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
  15. R语言学习——图形初阶之折线图与图形参数控制
  16. C# winform TreeView中关于checkbox选择的完美类[转]
  17. Hadoop之MapReduce思维导图
  18. Robot - 1. robot framework环境搭建
  19. IDEA如何自动生成testNG的测试报告?
  20. html-day04

热门文章

  1. float-position的一些细节
  2. Java线程的概念
  3. FPGA优化之高扇出
  4. OPP Services Log
  5. Python 简易聊天机器人
  6. 搭建Apache Web服务器
  7. 第3月第15天 afconvert lame
  8. Tomcat 内存优化设置
  9. 百度地图多点路径加载以及调整页面js
  10. BZOJ 3784: 树上的路径