最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法

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

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

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

來說明這個問題。

我們按照一般json格式生成,會出現(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)建三個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ù)轉(zhuǎn)換成json格式的字符串形式*/
		JSONArray array = new JSONArray();
		array = array.fromObject(list);
		System.out.println(array.toString());

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

但遇到Date類型的數(shù)據(jù)的時候,就需要進(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對象的格式化,以及數(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)建三個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ù)轉(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)用程序,會得到我們期望的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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot+Vue+axios實現(xiàn)文章收藏功能

    Springboot+Vue+axios實現(xiàn)文章收藏功能

    這篇文章主要為大家詳細(xì)介紹了Springboot+Vue+axios實現(xiàn)文章收藏功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • springboot如何配置允許跨域訪問

    springboot如何配置允許跨域訪問

    這篇文章主要介紹了springboot如何配置允許跨域訪問,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 最新評論

    土默特左旗| 白银市| 杭锦旗| 阿瓦提县| 乌什县| 乐业县| 渭源县| 额济纳旗| 铜鼓县| 丹寨县| 温宿县| 苗栗县| 辽中县| 镶黄旗| 连云港市| 泸州市| 龙州县| 通海县| 张家口市| 仪征市| 讷河市| 池州市| 宁津县| 介休市| 农安县| 永德县| 富顺县| 望江县| 土默特右旗| 平武县| 林西县| 申扎县| 江都市| 西和县| 图们市| 八宿县| 南平市| 乐山市| 昔阳县| 晋江市| 县级市|