json格式化自带类
package A.test; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor { private String format ="yyyy-MM-dd"; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK); return sdf.format(value); } return value == null ? "" : value.toString(); } }
2.测试类
package A.test; import java.text.ParseException; 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.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import net.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 class Json { // 不转换时间 // 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<Object> list = new ArrayList<Object>(); // 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 void main(String[] args) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> s1 = new HashMap<String, Object>(); s1.put("name", "jim"); s1.put("age", "15"); s1.put("time", new Date()); list.add(s1); Map<String, Object> s2 = new HashMap<String, Object>(); s2.put("name", "lucy"); s2.put("age", "12"); s2.put("time", new Date()); list.add(s2); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor()); JSONArray jo = JSONArray.fromObject(list, jsonConfig); System.out.println("json:" + jo.toString()); } }
3.测试类中的实体类
package A.test; import java.util.Date; public class Student { private String name; private String teacher; private Date time; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
4.自己反复测试过,100%可以使用。主要是针对java.util.Date时间格式.