Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法
想必我們?cè)谧鲰?xiàng)目的時(shí)候,都會(huì)遇到服務(wù)端與客戶端交互數(shù)據(jù)。一般情況下我們都會(huì)采用json格式或者xml格式,將服務(wù)端的數(shù)據(jù)轉(zhuǎn)換成這兩種格式之一。
但是,如果我們將數(shù)據(jù)轉(zhuǎn)換成json格式的時(shí)候,我們也許會(huì)遇到Date日期型的數(shù)據(jù)轉(zhuǎn)換成json格式后,并不是我們想要的格式。下面我們通過簡(jiǎn)單的demo
來說明這個(gè)問題。
我們按照一般json格式生成,會(huì)出現(xiàn)以下問題:
采用json:將數(shù)據(jù)生成json格式,需要導(dǎo)入相應(yīng)的jar包,如下圖:
Student.java
package com.xbmu.bean; import java.io.Serializable; import java.util.Date; public class Student implements Serializable { private String username; private Date birthday; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String username, Date birthday) { super(); this.username = username; this.birthday = birthday; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Student [username=" + username + ", birthday=" + birthday + "]"; } }
TestDateValueToJson.java
package com.xbmu.test; import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSONArray; import com.xbmu.bean.Student; public class TestDateValueToJson { public static void main(String[] args) { /** * 創(chuàng)建三個(gè)student對(duì)象,并將對(duì)象添加到List集合中 * * */ List<Student> list = new ArrayList<Student>(); Student student = new Student("張三", new Date()); list.add(student); student = new Student("李四",new Date()); list.add(student); student = new Student("王五",new Date()); list.add(student); /**將list集合眾的數(shù)據(jù)轉(zhuǎn)換成json格式的字符串形式*/ JSONArray array = new JSONArray(); array = array.fromObject(list); System.out.println(array.toString());
運(yùn)行Java應(yīng)用程序,看見在控制臺(tái)是哪個(gè)打印出了:(這里通過json格式化工具處理后了,方便大家閱讀)
[ { "birthday": { "date": 3, "day": 4, "hours": 9, "minutes": 5, "month": 11, "seconds": 1, "time": 1449104701018, "timezoneOffset": -480, "year": 115 }, "username": "張三" }, { "birthday": { "date": 3, "day": 4, "hours": 9, "minutes": 5, "month": 11, "seconds": 1, "time": 1449104701018, "timezoneOffset": -480, "year": 115 }, "username": "李四" }, { "birthday": { "date": 3, "day": 4, "hours": 9, "minutes": 5, "month": 11, "seconds": 1, "time": 1449104701018, "timezoneOffset": -480, "year": 115 }, "username": "王五" } ]
雖然符合json語法格式,但是里面的birthday字段是日期型的,并不是我們一般情況下需要的。這時(shí)候,我們就必須寫一個(gè)工具類進(jìn)行處理了。
但遇到Date類型的數(shù)據(jù)的時(shí)候,就需要進(jìn)行處理。
package com.xbmu.utils; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; /** * 自定義JsonValueProcessor * 比如我們要控制JSON序列化過程中的Date對(duì)象的格式化,以及數(shù)值的格式化,JsonValueProcessor是最好的選擇。 * @author bitaotao * */ public class JsonDateValueProcessor implements JsonValueProcessor { private String pattern = "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(pattern, Locale.UK); return sdf.format(value); } return value == null ? "" : value.toString(); } }
除了自定義日期格式外,還可以如法炮制,控制數(shù)值格式化、HTML內(nèi)容轉(zhuǎn)碼等。
TestDateValueToJson.java
package com.xbmu.test; import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; import com.xbmu.bean.Student; import com.xbmu.utils.JsonDateValueProcessor; public class TestDateValueToJson { public static void main(String[] args) { /** * 創(chuàng)建三個(gè)student對(duì)象,并將對(duì)象添加到List集合中 * * */ List<Student> list = new ArrayList<Student>(); Student student = new Student("張三", new Date()); list.add(student); student = new Student("李四",new Date()); list.add(student); student = new Student("王五",new Date()); list.add(student); /**將list集合眾的數(shù)據(jù)轉(zhuǎn)換成json格式的字符串形式*/ JsonConfig config = new JsonConfig(); JsonDateValueProcessor jsonValueProcessor = new JsonDateValueProcessor(); config.registerJsonValueProcessor(Date.class, jsonValueProcessor); JSONArray array = new JSONArray(); array = array.fromObject(list,config); System.out.println(array.toString()); } }
運(yùn)行Java應(yīng)用程序,會(huì)得到我們期望的json格式:
[ { "birthday": "2015-12-03", "username": "張三" }, { "birthday": "2015-12-03", "username": "李四" }, { "birthday": "2015-12-03", "username": "王五" } ]
很顯然這種日期格式,是我們經(jīng)常使用的。也方便在客戶端解析這種格式的json字符串。
總結(jié)
到此這篇關(guān)于Java將Date日期類型字段轉(zhuǎn)換成json字符串的文章就介紹到這了,更多相關(guān)Java Date日期類型字段轉(zhuǎn)json字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系
這篇文章主要介紹了關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02淺談Java實(shí)現(xiàn)分布式事務(wù)的三種方案
現(xiàn)在互聯(lián)網(wǎng)下,分布式和微服務(wù)橫行,難免會(huì)遇到分布式下的事務(wù)問題,當(dāng)然微服務(wù)下可能沒有分布式事務(wù),但是很多場(chǎng)景是需要分布式事務(wù)的。下面就來介紹下什么是分布式事務(wù)和分布式事務(wù)的解決方案2021-06-06java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06idea快速搭建springboot項(xiàng)目的操作方法
下面小編就為大家分享一篇idea快速搭建springboot項(xiàng)目的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12SpringBoot整合Mybatis自定義攔截器不起作用的處理方案
這篇文章主要介紹了SpringBoot整合Mybatis自定義攔截器不起作用的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09