Sometimes it is necessary to return some json data to the front desk, or a json array. Through the fromObject method provided by json, we can easily encapsulate the complete json data, but when encountering some cascading attributes (I don't know if this is correct), For example, Date that records the time, etc., directly using the fromObject method will report an error, or the result will not appear in the format we need!
First introduce the json package , just Baidu (I use json-lib), I learned about a fastjson package of Alibaba Cloud when I asked for advice today, but I haven't researched it yet. It is said to be very powerful. I will study it later. Will write a blog! (Because I import many commonly used packages at one time, so I don't take screenshots, but now I'm gradually starting to write projects with maven)
First, we need to implement the JsonValueProcessor interface provided by json and specify the processing method of the Date type! Code from Baidu
package com.loger.test; import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor { // Define the output format of the converted date type private String format = "yyyy-MM-dd" ; public JsonDateValueProcessor() { } public JsonDateValueProcessor(String format) { this.format = format; } @Override public Object processArrayValue(Object arg0, JsonConfig arg1) { return process(arg0); } private Object process(Object arg0) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(arg0); } @Override public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof java.util.Date) { String str = new SimpleDateFormat(format).format((Date) value); return str; } if (null != value) { return value.toString(); } return ""; } }
Test JSONArray: (later note: I accidentally made a mistake here, use uppercase MM, lowercase for points)
package com.loger.test; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.loger.entity.UserDate; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; public class JSONArrayTestForDate { public static List<UserDate> list = new ArrayList<>(); static { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); Date birthday; try { birthday = sdf.parse("1995-09-01"); System.out.println(birthday); list.add(new UserDate("chenle",birthday)); list.add(new UserDate("chenle",birthday)); list.add(new UserDate("chenle",birthday)); list.add(new UserDate("chenle",birthday)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig); System.out.println(jsonArray); } }
运行结果:
可以看到,日期已经按照指定的类型输出
测试JSONObject:
package com.loger.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.loger.entity.UserDate; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class JSONObjectTestForDate { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); UserDate user = new UserDate("chenle",sdf.parse("1995-09-01")); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); JSONObject jsonObject = new JSONObject(); jsonObject = JSONObject.fromObject(user,jsonConfig); System.out.println(jsonObject); } }
operation result:
The following results in a test that does not process the format:
The result of not processing is the same
Also say:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); User user = new User("chenle",sdf.parse("1995-09-01"));
The above format is used for the input of the date. The new Date("1995-09-01") method shows that it has expired, and an error will be reported!