依赖包@H_403_3@
json-lib-2.4-jdk15.jar 可以到http://download.csdn.net/detail/sigangjun/5915019下载
编写Util类@H_403_3@
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,total); map.put("rows",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); List<People> childrenlist = new ArrayList<People>(); People p3 = new People("001","我是男孩",null); People p4 = new People("002","我是女孩",null); Map<String,People> friends = new HashMap<String,People>(); People p5 = new People("003","我是朋友1",null); People p6 = new People("004","我是朋友2",null); friends.put("111",p5); friends.put("222",p6); People p2 = new People("123","打印我吧!",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类@H_403_3@
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 } }