packageA.test;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importnet.sf.json.JSON;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importnet.sf.json.JSONSerializer;importnet.sf.json.JsonConfig;//转换时间格式后--需要commons-beanutils.jar,commons-collections-3.2.1.jar,commons-lang.jar,commons-logging-1.0.4.jar//ezmorph-1.0.1.jar,json-lib-2.4-jdk15.jar
public classJson {//不转换时间//public static void main(String[] args) {//Student st = new Student();//st.setName("张翠山");//st.setTeacher("张三丰");//st.setTime(new Date());//JSON jo = JSONSerializer.toJSON(st);//System.out.println("json:" + jo.toString());//}//对象转换//public static void main(String[] args) {//Student st = new Student();//st.setName("张翠山");//st.setTeacher("张三丰");//st.setTime(new Date());//
//JsonConfig jsonConfig = new JsonConfig();// //JsonConfig是net.sf.json.JsonConfig中的这个,为固定写法//jsonConfig.registerJsonValueProcessor(Date.class , new//JsonDateValueProcessor());//JSONArray jo = JSONArray.fromObject(st, jsonConfig);//System.out.println("json:" + jo.toString());//}//集合转换//public static void main(String[] args) {//List list = new ArrayList();//Date date = new Date();//System.out.println(date);//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");//String s= sdf.format(date);//System.out.println(s);// //try {// //Date d = sdf.parse(s);// //System.out.println(d);//list.add(0,s);// //} catch (ParseException e) {// //TODO Auto-generated catch block// //e.printStackTrace();// //}//list.add(1,"张三");//list.add(2,2);//list.add(3,"李四");//list.add(4,date);//JsonConfig jsonConfig = new JsonConfig();//JsonConfig是net.sf.json.JsonConfig中的这个,为固定写法//jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());//JSONArray jo = JSONArray.fromObject(list, jsonConfig);//System.out.println("json:" + jo.toString());//}//集合转换
public static voidmain(String[] args) {
List> list = new ArrayList>();
Map s1 = new HashMap();
s1.put("name", "jim");
s1.put("age", "15");
s1.put("time", newDate());
list.add(s1);
Map s2 = new HashMap();
s2.put("name", "lucy");
s2.put("age", "12");
s2.put("time", newDate());
list.add(s2);
JsonConfig jsonConfig= newJsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class , newJsonDateValueProcessor());
JSONArray jo=JSONArray.fromObject(list, jsonConfig);
System.out.println("json:" +jo.toString());
}
}