java对象转换成json
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;
}
} }
java对象转换成json的更多相关文章
- Java对象转换成xml对象和Java对象转换成JSON对象
1.把Java对象转换成JSON对象 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串.也可以把JavaBean转换成JSON字符串. json-lib的核心 ...
- Java对象转换成Json字符串是无法获得对应字段名
问题: 代码中已经标注 @JSONField(name = "attrs") private String abc; public String getA() { return a ...
- 如何将java对象转换成json数据
package cn.hopetesting.com.test;import cn.hopetesting.com.domain.User;import com.fasterxml.jackson.c ...
- FastJson将Java对象转换成json
确保环境依赖都配置好! 1.在pom.xml导入依赖 <dependency> <groupId>com.alibaba</groupId> <artifac ...
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- json 字符串转换成对象,对象转换成json字符串
json 字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法: [注意jquery版本问题] var str = '{"name":&qu ...
- DataTabel DataSet 对象 转换成json
public class DataTableConvertJson { #region dataTable转换成Json格式 /// <summary> ...
- JSON对象转换成JSON字符串
1.问题背景 有一个json对象,需要将其转换成json字符串 JSON.stringify(obj) 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DT ...
- 将Model对象转换成json文本或者json二进制文件
将Model对象转换成json文本或者json二进制文件 https://github.com/casatwy/AnyJson 注意:经过测试,不能够直接处理字典或者数组 主要源码的注释 AJTran ...
随机推荐
- layer——源码学习
一.根据源码的学习 发现创建弹窗:使用了一些div来组成 zindex 和 index 是自动生成. zindex 表示生成的层次关系 index 用来表示各个层的id 默认class名 h = [& ...
- [译]Writing Custom Middleware in ASP.NET Core 1.0
原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...
- vtkTransform实例 1
1. 4*4矩阵类vtkMatrix4x4 接口函数:void SetElement(int i, int j, double value),i行.j列的值为value #ifndef INITIAL ...
- SQL语句生成指定范围内随机数
1.生成随机实型数据 create procedure awf_RandDouble @min dec(14,2), @max dec(14,2), @result dec(14,2) output ...
- Eclipse 中文的设置
步骤如下:一.下载:在Eclipse官网下载相应版本的中文包. 二.中文包安装:1.解压中文语言包中的两个文件夹至Eclipse文件夹的dropins文件夹中.(目录不要放错)2.安装:方法一:使用命 ...
- HTML5、微信、APP:创业寒冬只能选其一,该选哪个?
HTML5手机网站 优势:开发技术简单,研发周期短,用户接触成本低 劣势:功能实现相比APP存在差距,用户重复使用难度大,用户粘性差 适合场景:把手机网站当成网络上的“电子产品介绍手册”.手机网站更适 ...
- iptables原理
1.iptables防火墙简介 Iptables也叫netfilter是Linux下自带的一款免费且优秀的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入.流出.流经服务器的数据包进 ...
- 用VC2010以上版本编译可以在低版本XP和2003的运行程序的方法
2013-09-17 作者:佚名 来源:本站整理 浏览:2001 评论:1 一直以来倍受此事困拢,vc2010以上版本编译出的exe或dll总是会引用kernel32.dll的En ...
- 把两个table放在一个Repeater中显示
DataTable dt; DataTable dt1; HLoanApplyInfo applyInfo = HLoanApplyBll.GetModelById(FLoanID); FLoanID ...
- Menu与ActionBar的爱恨情仇
最近在开发一款音乐播放器,在开发过程中遇到了一点小麻烦,通过android API搞清楚了Menu与ActionBar的爱恨情仇,写了个小Demo祭奠一下那些年我们陷进去的坑,有不对的地方请大神们批评 ...