亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java將Date日期類型字段轉換成json字符串的方法

 更新時間:2021年02月06日 14:48:45   作者:上善若水  
這篇文章主要給大家介紹了關于Java將Date日期類型字段轉換成json字符串的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

想必我們在做項目的時候,都會遇到服務端與客戶端交互數(shù)據(jù)。一般情況下我們都會采用json格式或者xml格式,將服務端的數(shù)據(jù)轉換成這兩種格式之一。

但是,如果我們將數(shù)據(jù)轉換成json格式的時候,我們也許會遇到Date日期型的數(shù)據(jù)轉換成json格式后,并不是我們想要的格式。下面我們通過簡單的demo

來說明這個問題。

我們按照一般json格式生成,會出現(xiàn)以下問題:

采用json:將數(shù)據(jù)生成json格式,需要導入相應的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)建三個student對象,并將對象添加到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ù)轉換成json格式的字符串形式*/
		JSONArray array = new JSONArray();
		array = array.fromObject(list);
		System.out.println(array.toString());

運行Java應用程序,看見在控制臺是哪個打印出了:(這里通過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字段是日期型的,并不是我們一般情況下需要的。這時候,我們就必須寫一個工具類進行處理了。

但遇到Date類型的數(shù)據(jù)的時候,就需要進行處理。

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對象的格式化,以及數(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內容轉碼等。

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)建三個student對象,并將對象添加到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ù)轉換成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());
	}
}	

運行Java應用程序,會得到我們期望的json格式:

[
 {
  "birthday": "2015-12-03",
  "username": "張三"
 },
 {
  "birthday": "2015-12-03",
  "username": "李四"
 },
 {
  "birthday": "2015-12-03",
  "username": "王五"
 }
]

很顯然這種日期格式,是我們經(jīng)常使用的。也方便在客戶端解析這種格式的json字符串。

總結

到此這篇關于Java將Date日期類型字段轉換成json字符串的文章就介紹到這了,更多相關Java Date日期類型字段轉json字符串內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 配置SpringBoot方便的切換jar和war的方法示例

    配置SpringBoot方便的切換jar和war的方法示例

    這篇文章主要介紹了配置SpringBoot方便的切換jar和war的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 關于@Autowired注解和靜態(tài)方法及new的關系

    關于@Autowired注解和靜態(tài)方法及new的關系

    這篇文章主要介紹了關于@Autowired注解和靜態(tài)方法及new的關系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 淺談Java實現(xiàn)分布式事務的三種方案

    淺談Java實現(xiàn)分布式事務的三種方案

    現(xiàn)在互聯(lián)網(wǎng)下,分布式和微服務橫行,難免會遇到分布式下的事務問題,當然微服務下可能沒有分布式事務,但是很多場景是需要分布式事務的。下面就來介紹下什么是分布式事務和分布式事務的解決方案
    2021-06-06
  • 輕松掌握java外觀模式

    輕松掌握java外觀模式

    這篇文章主要幫助大家輕松掌握java外觀模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • java中for循環(huán)執(zhí)行的順序圖文詳析

    java中for循環(huán)執(zhí)行的順序圖文詳析

    關于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關于java中for循環(huán)執(zhí)行順序的相關資料,需要的朋友可以參考下
    2021-06-06
  • idea快速搭建springboot項目的操作方法

    idea快速搭建springboot項目的操作方法

    下面小編就為大家分享一篇idea快速搭建springboot項目的操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 非常全面的IReport的使用教程

    非常全面的IReport的使用教程

    iReport 是為JasperReports Library和JasperReports Server設計的報表可視化設計器。本教程給大家詳細介紹IReport的使用解析,感興趣的朋友一起看看吧
    2021-10-10
  • SpringBoot整合Mybatis自定義攔截器不起作用的處理方案

    SpringBoot整合Mybatis自定義攔截器不起作用的處理方案

    這篇文章主要介紹了SpringBoot整合Mybatis自定義攔截器不起作用的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 如何使用Java在excel單元格中設置超鏈接

    如何使用Java在excel單元格中設置超鏈接

    這篇文章主要介紹了如何使用Java在excel單元格中設置超鏈接,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • java版十大排序經(jīng)典算法:完整代碼

    java版十大排序經(jīng)典算法:完整代碼

    優(yōu)秀的文章也不少,但是Java完整版的好像不多,我把所有的寫一遍鞏固下,同時也真誠的希望閱讀到這篇文章的小伙伴們可以自己去從頭敲一遍,不要粘貼復制!希望我的文章對你有所幫助,每天進步一點點
    2021-07-07

最新評論