可能會遇到這樣一個問題,咱們將數據以json字符串的形式傳給頁面的時候,有可能數據格式中包含比較複雜的格式,如:日期,這樣,在頁面中解析json數據的時候,不方便進行處理(也不是不能處理),因此,咱們但願將複雜的格式,以某種簡單的方式傳給頁面。java
public class User { private int id; private String name; private Date date; public User(int id, String name, Date date) { super(); this.id = id; this.name = name; this.date = date; }
在User類中,假設咱們有一個Date類型的日期數據。ajax
User u1 = new User(1,"sucan",new Date()); User u2 = new User(2,"ximi",new Date()); User u3 = new User(3,"sc",new Date()); List<User> list = new ArrayList<User>(); list.add(u1); list.add(u2); list.add(u3); JSONArray arr = JSONArray.fromObject(list); String user = arr.toString(); PrintWriter pw = response.getWriter(); pw.println(user); pw.flush(); pw.close();
$(function(){ $.ajax({ method:"get", url:"JsonServlet", data:null, success:function(msg){ var data = JSON.parse(msg); console.info(data); }, error:function(){ alert("數據請求錯誤!"); } }); });
[{"date":{"date":26,"day":1,"hours":18,"minutes":58,"month":11,"seconds":35,"time":1482749915563,"timezoneOffset":-480,"year":116},"id":1,"name":"sucan"},{"date":{"date":26,"day":1,"hours":18,"minutes":58,"month":11,"seconds":35,"time":1482749915563,"timezoneOffset":-480,"year":116},"id":2,"name":"ximi"},{"date":{"date":26,"day":1,"hours":18,"minutes":58,"month":11,"seconds":35,"time":1482749915563,"timezoneOffset":-480,"year":116},"id":3,"name":"sc"}]
能夠看到,日期格式很複雜,這樣,咱們頁面取值的時候,可能就會變得很複雜,因而,咱們但願將日期,以一種簡單的方式傳給jsp頁面(如:XXXX-XX-XX的形式)。json
JsonConfig config = new JsonConfig(); JSONArray arr = JSONArray.fromObject(list, config);
config.registerJsonValueProcessor(java.util.Date.class, jsonValueProcessor);
如今關鍵是第二個參數,因此咱們要實現jsonValueProcessor接口;jsp
public class jsonValueProcessorImpl implements JsonValueProcessor { public Object processArrayValue(Object arg0, JsonConfig arg1) { // TODO Auto-generated method stub return null; } public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) { // TODO Auto-generated method stub return null; } }
能夠看到,這個接口包含兩個未實現的方法,下面咱們就是要寫這兩個方法。 arg0:表示鍵類型; arg1:表示往json格式中傳的value值; arg2:默認配置對象函數
public Object processObjectValue(String key, Object value, JsonConfig arg2) { if(value instanceof java.util.Date){ Date d = (Date)value; return new SimpleDateFormat(pattern).format(d); } return null; }
其中,pattern是咱們默認給的格式,也能夠在構造器中動態設置測試
String pattern = "yyyy-MM-dd"; public jsonValueProcessorImpl(){ } public jsonValueProcessorImpl(String pattern){ this.pattern = pattern; }
最後,咱們在servlet中new jsonValueProcessorImpl()實現類this
config.registerJsonValueProcessor(java.util.Date.class, new jsonValueProcessorImpl());
Object date:"2016-12-26" id:1 name:"sucan" __proto__:Object 1:Object date:"2016-12-26" id:2 name:"ximi" __proto__:Object 2:Object date:"2016-12-26" id:3 name:"sc"
咱們能夠看到,咱們的日期已經變成了咱們設置好的格式,而且是一個字符串形式。這樣咱們取值便方便了不少。url
Date u1 = new Date(); Date u2 = new Date(); Date u3 = new Date(); List list = new ArrayList(); list.add(u1); list.add(u2); list.add(u3);
[null, null, null] 0:null 1:null 2:null
爲何頁面取值全是null呢?由於如今對象中除了Date對象不包含任何其餘數據,默認會執行,咱們上面所說未實現方法的第一個方法,因而,咱們爲了更好的兼容性,咱們將在第二個方法中所寫的代碼,最好仍是在第一個方法中重寫一次。爲了不重複寫代碼,咱們能夠將代碼封裝成一個方法,而後在實現類的方法中分別調用這個方法code
public Object processArrayValue(Object value, JsonConfig arg1) { return hanlder(value); } public Object processObjectValue(String key, Object value, JsonConfig arg2) { return hanlder(value); } public String hanlder(Object value){ if(value instanceof java.util.Date){ Date d = (Date)value; return new SimpleDateFormat(pattern).format(d); } return null; }
["2016-12-26", "2016-12-26", "2016-12-26"]