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":"打印我吧!"}]}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 当正职直接指挥下级副职领导怎么办 单位申报个税身份证号码填错怎么办 申报个税身份证号码填错了怎么办 办了人事代理联系函商调函怎么办 劳务公司与分包工头算账成怎么办 客户退保业务员己得保险佣金怎么办 投了简历公司打来电话没接到怎么办 跟老板要工资她说过几天怎么办 公路车弯把手小抓不到刹车怎么办 新手骑电动车上路车子一直晃怎么办 不知情收赃东西已经卖了怎么办 档案和派遣证不在同一个地方怎么办 档案缺招工表和派遣证怎么办退休 开发商对接的银行利息比较高怎么办 工作未满一年离职转正定级怎么办 被网络骗贷翻到几十万怎么办 面试通过后迟迟不通知入职怎么办 厂里捡到饭卡花了里面的钱怎么办 上班用自己手机打卡没电怎么办 逸尚考勤机显示超出考勤记录怎么办 下雨穿套裙工装上班好冷怎么办 蚂蚁借呗放款中不到账怎么办 护士电子化注册没有激活码怎么办? 地铁站务员入职体检身高差点怎么办 车子钥匙锁在车里了怎么办 打错账户了怎么办5天了 下岗失业人员在就业后档案怎么办 皮秒祛斑后没有敷面膜怎么办 硕士毕业工资好低不想工作怎么办 车卖了etc忘拿了怎么办 成熟卵泡打破卵针也不破怎么办? 深圳孩子户口挂别人名下上学怎么办 襄阳东风合运花园房贷怎么办下来 猫在外面躲起来找不到了怎么办 本科三批取消以后三本学校怎么办 机票名字多了个字母安检怎么办 出隧道口限速40超速了怎么办 社保交了五年后断交以后该怎么办 躺椅折叠椅坐的地方坏了怎么办 苹果手机锁频密码忘了怎么办 客户要货公司不给进怎么办