想必我们在做项目的时候,都会遇到服务端与客户端交互数据。一般情况下我们都会采用json格式或者xml格式,将服务端的数据转换成这两种格式之一。

但是,如果我们将数据转换成json格式的时候,我们也许会遇到date日期型的数据转换成json格式后,并不是我们想要的格式。下面我们通过简单的demo

来说明这个问题。

我们按照一般json格式生成,会出现以下问题:

采用json:将数据生成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) {
		/**
		 * 创建三个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集合众的数据转换成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类型的数据的时候,就需要进行处理。

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对象的格式化,以及数值的格式化,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();
	}
 
}

除了自定义日期格式外,还可以如法炮制,控制数值格式化、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) {
		/**
		 * 创建三个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集合众的数据转换成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": "王五"
 }
]

很显然这种日期格式,是我们经常使用的。也方便在客户端解析这种格式的json字符串。

总结

到此这篇关于java将date日期类型字段转换成json字符串的文章就介绍到这了,更多相关java date日期类型字段转json字符串内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!