博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JsonUtils工具类 把java对象转成Json串的工具类
阅读量:4078 次
发布时间:2019-05-25

本文共 5176 字,大约阅读时间需要 17 分钟。

依赖包

json-lib-2.4-jdk15.jar 可以到下载

编写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;/** * 

Title: json util

*

Description: this util can covert object to jsonstring

* @since 2013-8-12 下午3:32:19 * @version 1.0 * @author sigangjun */public class JsonUtils { static JsonConfig jsonConfig = new JsonConfig(); static { jsonConfig.registerJsonValueProcessor(Date.class,new JsonValueProcessor(){ @Override public 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 ""; } } @Override public 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
map = new HashMap
(); 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
map = new HashMap
(); 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
list = Lists.newArrayList(); Business bb = new Business();*/ List
alllist = new ArrayList
(); People p1 = new People("000", "我是facher", new Date(), null, null, null); List
childrenlist = new ArrayList
(); People p3 = new People("001", "我是男孩", new Date(), null, null, null); People p4 = new People("002", "我是女孩", new Date(), null, null, null); Map
friends = new HashMap
(); 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;/** * 

Title: People class

*

Description: People Class

* @since 2013-8-12 下午3:39:38 * @version 1.0 * @author sigangjun */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
children; private Map
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
getChildren() { return children; } public void setChildren(List
children) { this.children = children; } public Map
getFriends() { return friends; } public void setFriends(Map
friends) { this.friends = friends; } public static long getSerialversionuid() { return serialVersionUID; } public People(String id, String name, Date birthday, People father, List
children, Map
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":"打印我吧!"}]}

你可能感兴趣的文章
Android应用界面开发_学习笔记_第三周
查看>>
AS快捷键
查看>>
Fragment
查看>>
Handler
查看>>
Service
查看>>
BroadcastReceiver
查看>>
Widget
查看>>
Android应用界面开发_学习笔记_第四周
查看>>
SharedPreferences
查看>>
管理文件
查看>>
Android网络与数据存储_学习笔记_第五周
查看>>
Android网络与数据存储_学习笔记_第六周:SQLite与ContentProvider
查看>>
网络编程数据处理_学习笔记_第七周
查看>>
Android UiAutomator UiObject API
查看>>
多线程_学习笔记_第七周
查看>>
java基础 -- 六哥
查看>>
多进程
查看>>
传感器与LBS
查看>>
接口测试关注点、常用工具
查看>>
应用性能优化
查看>>