JsonUtils工具类 把java对象转成Json串的工具类

依赖包

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(){

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值