JsonUtils工具类 把java对象转成Json串的工具类
来源:互联网 发布:淘宝客服个人工作经验 编辑:程序博客网 时间:2024/05/19 04:51
依赖包
json-lib-2.4-jdk15.jar 可以到http://download.csdn.net/detail/sigangjun/5915019下载
编写Util类
package cn.sigangjun.util;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;import cn.sigangjun.model.People;/** * <p>Title: json util</p> * <p>Description: this util can covert object to jsonstring</p> * @since 2013-8-12 下午3:32:19 * @version 1.0 * @author <a style='color:red' href='http://blog.csdn.net/sigangjun'>sigangjun</a> */public class JsonUtils {static JsonConfig jsonConfig = new JsonConfig();static {jsonConfig.registerJsonValueProcessor(Date.class,new JsonValueProcessor(){@Overridepublic Object processArrayValue(Object value, JsonConfig arg1) {try { if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format((Date) value); } return value == null ? "" : value.toString(); } catch (Exception e) { return ""; }}@Overridepublic Object processObjectValue(String arg0, Object arg1,JsonConfig arg2) {try { if (arg1 instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format((Date) arg1); } return arg1 == null ? "" : arg1.toString(); } catch (Exception e) { return ""; }}});}@SuppressWarnings("rawtypes")public static String printJsonForTree(List list) {Map<String, Object> map = new HashMap<String, Object>();map.put("total", list.size());map.put("rows", list);JSONObject json = JSONObject.fromObject(map, jsonConfig);return json.toString();}/** * 输出Json串 * * @param list 业务集 * @param total 总数 * @return String Json串 */public static String outJsonString (List<?> list, Integer total) {if (list == null || total == null) {return "";}Map<String, Object> map = new HashMap<String, Object>();map.put("total", total);map.put("rows", list);JSONObject json = JSONObject.fromObject(map, jsonConfig);return json.toString();}/** * @param args */public static void main(String[] args) {/*List<Business> list = Lists.newArrayList();Business bb = new Business();*/List<People> alllist = new ArrayList<People>();People p1 = new People("000", "我是facher", new Date(), null, null, null);List<People> childrenlist = new ArrayList<People>();People p3 = new People("001", "我是男孩", new Date(), null, null, null);People p4 = new People("002", "我是女孩", new Date(), null, null, null);Map<String, People> friends = new HashMap<String, People>();People p5 = new People("003", "我是朋友1", new Date(), null, null, null);People p6 = new People("004", "我是朋友2", new Date(), null, null, null);friends.put("111", p5);friends.put("222", p6);People p2 = new People("123", "打印我吧!", new Date(),p1, childrenlist, friends);alllist.add(p2);System.out.println(printJsonForTree(alllist));}public static String beanToJson(Object bean) {JSONObject obj = JSONObject.fromObject(bean);return obj.toString();}}
编写People类
package cn.sigangjun.model;import java.io.Serializable;import java.util.Date;import java.util.List;import java.util.Map;/** * <p>Title: People class</p> * <p>Description: People Class</p> * @since 2013-8-12 下午3:39:38 * @version 1.0 * @author <a style='color:red' href='http://blog.csdn.net/sigangjun'>sigangjun</a> */public class People implements Serializable {private static final long serialVersionUID = 1L;private String id;private String name;private Date birthday;private People father;private List<People> children;private Map<String, People> friends;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public People getFather() {return father;}public void setFather(People father) {this.father = father;}public List<People> getChildren() {return children;}public void setChildren(List<People> children) {this.children = children;}public Map<String, People> getFriends() {return friends;}public void setFriends(Map<String, People> friends) {this.friends = friends;}public static long getSerialversionuid() {return serialVersionUID;}public People(String id, String name, Date birthday, People father,List<People> children, Map<String, People> friends) {super();this.id = id;this.name = name;this.birthday = birthday;this.father = father;this.children = children;this.friends = friends;}public People() {super();// TODO Auto-generated constructor stub}}
运行结果
{"total":1,"rows":[{"birthday":"2013-08-12 15:43:57","children":[],"father":{"birthday":"2013-08-12 15:43:57","children":[],"father":null,"friends":null,"id":"000","name":"我是facher"},"friends":{"222":{"birthday":"2013-08-12 15:43:57","children":[],"father":null,"friends":null,"id":"004","name":"我是朋友2"},"111":{"birthday":"2013-08-12 15:43:57","children":[],"father":null,"friends":null,"id":"003","name":"我是朋友1"}},"id":"123","name":"打印我吧!"}]}
- JsonUtils工具类 把java对象转成Json串的工具类
- Java 的 JSON 开源类库之JackSon工具类--JSONUtils
- Java 的 JSON 开源类库之JackSon工具类--JSONUtils
- 封装类与json的转换的工具类JsonUtils
- JsonUtils工具类
- JSONUtils 工具类
- 使用第三方工具把对象转成json串
- java工具类开发之json工具类JsonUtils和写数据回页面工具类ResponseUtils
- 工具类--JsonUtils 数据解析
- JsonUtils ~~fastJson解析工具类
- JAVA中封装JSONUtils工具类及使用
- 在JAVA中封装JSONUtils工具类及使用
- 在JAVA中封装JSONUtils工具类及使用
- 在JAVA中封装JSONUtils工具类及使用
- 在JAVA中封装JSONUtils工具类及使用
- 在JAVA中封装JSONUtils工具类及使用
- 封装JSONUtils工具类及使用
- Web 开发工具类(3): JsonUtils
- FromHandle
- fck在上传文件的时候 FCKeditor 报错
- 一个单例
- picture control控件
- Mybatis 3.1中 Mapper XML 文件 的学习详解
- JsonUtils工具类 把java对象转成Json串的工具类
- Dalvik opcodes
- Adobe交互式PDF表单的优化
- armlinux下的网路传输(tcp/ip)
- android SystemUI浅析之SystemUI启动流程
- ORA的资源限制的设置
- linux-tar分卷压缩与解压缩
- sidetone
- DRM in Android