JSON格式化,详细介绍!

对于date类型的转化一直是json中比较复杂的事情,我这只提出一种自己会的方式与大家分享,如果还好更好的方式别忘记告诉我学习下嘿嘿在这先谢了

 

对于date类型直接转化如下

 

Java代码 技术分享 技术分享
  1. java.util.Date testDate = new Date();  
  2. JSONObject jsonFromDate = JSONObject.fromObject(testDate);  
  3. System.out.println(jsonFromDate);  
  4. //prints {"date":26,"day":1,"hours":11,"minutes":30,"month":9,"seconds":18,"time":1256527818296,"timezoneOffset":-480,"year":109} 
		java.util.Date testDate = new Date();
		JSONObject jsonFromDate = JSONObject.fromObject(testDate);
		System.out.println(jsonFromDate);
		//prints {"date":26,"day":1,"hours":11,"minutes":30,"month":9,"seconds":18,"time":1256527818296,"timezoneOffset":-480,"year":109}

 

上述转化后的格式明显不利于我们使用,下面介绍我的方法

 

 

Java代码 技术分享 技术分享
  1. //注册date类型的转化方式  
  2. JsonConfig jsonConfig = new JsonConfig();  
  3. jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImplTest());  
  4.    
  5. JSONObject jsonFromBean = JSONObject.fromObject(testBean,jsonConfig);  
  6. System.out.println(jsonFromBean);  
  7.  
  8. //prints {"birthday":"2009-10-26","id":"id","name":"name"}  
  9.  
  10. String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"};   
  11. JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));   
  12. TestBean jsonToBean = (TestBean)JSONObject.toBean(jsonFromBean,TestBean.class);  
  13. System.out.println(jsonToBean);  
  14. //prints TestBean@1126b07[id=id,name=name,birthday=Mon Oct 26 00:00:00 CST 2009] 
		//注册date类型的转化方式
		 JsonConfig jsonConfig = new JsonConfig();
		 jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImplTest());
		 
		JSONObject jsonFromBean = JSONObject.fromObject(testBean,jsonConfig);
		System.out.println(jsonFromBean);
		
		//prints {"birthday":"2009-10-26","id":"id","name":"name"}
		
		String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"}; 
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats)); 
		TestBean jsonToBean = (TestBean)JSONObject.toBean(jsonFromBean,TestBean.class);
		System.out.println(jsonToBean);
		//prints TestBean@1126b07[id=id,name=name,birthday=Mon Oct 26 00:00:00 CST 2009]

 

其中需要的类如下:

 

1.准备测试数据

 

Java代码 技术分享 技术分享
  1. import java.util.Date;  
  2.  
  3. import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
  4.  
  5. public class TestBean {  
  6.  
  7.     private String id;  
  8.     private String name;  
  9.     private java.util.Date birthday;  
  10.  
  11.     public TestBean() {  
  12.         super();  
  13.     }  
  14.  
  15.     public TestBean(String id, String name, Date birthday) {  
  16.         super();  
  17.         this.id = id;  
  18.         this.name = name;  
  19.         this.birthday = birthday;  
  20.     }  
  21.  
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.  
  26.     public void setId(String id) {  
  27.         this.id = id;  
  28.     }  
  29.  
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.  
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.  
  38.     public java.util.Date getBirthday() {  
  39.         return birthday;  
  40.     }  
  41.  
  42.     public void setBirthday(java.util.Date birthday) {  
  43.         this.birthday = birthday;  
  44.     }  
  45.  
  46.     public String toString() {  
  47.         return ReflectionToStringBuilder.toString(this);  
  48.     }  
  49.  
import java.util.Date;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

public class TestBean {

	private String id;
	private String name;
	private java.util.Date birthday;

	public TestBean() {
		super();
	}

	public TestBean(String id, String name, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.birthday = birthday;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public java.util.Date getBirthday() {
		return birthday;
	}

	public void setBirthday(java.util.Date birthday) {
		this.birthday = birthday;
	}

	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

}

 

 

2.创建date格式化类

 

Java代码 技术分享 技术分享
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3.  
  4. import net.sf.json.JsonConfig;  
  5. import net.sf.json.processors.JsonValueProcessor;  
  6.  
  7. public class JsonValueProcessorImplTest implements JsonValueProcessor {  
  8.     private String format = "yyyy-MM-dd";  
  9.  
  10.       
  11.     public JsonValueProcessorImplTest() {  
  12.         super();  
  13.     }  
  14.  
  15.     public JsonValueProcessorImplTest(String format) {  
  16.         super();  
  17.         this.format = format;  
  18.     }  
  19.  
  20.     @Override 
  21.     public Object processArrayValue(Object value, JsonConfig jsonConfig) {  
  22.         String[] obj = {};  
  23.         if (value instanceof Date[]) {  
  24.             SimpleDateFormat sf = new SimpleDateFormat(format);  
  25.             Date[] dates = (Date[]) value;  
  26.             obj = new String[dates.length];  
  27.             for (int i = 0; i < dates.length; i++) {  
  28.                 obj[i] = sf.format(dates[i]);  
  29.             }  
  30.         }  
  31.         return obj;  
  32.     }  
  33.  
  34.     @Override 
  35.     public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {  
  36.         if (value instanceof java.util.Date) {  
  37.             String str = new SimpleDateFormat(format).format((Date) value);  
  38.             return str;  
  39.         }  
  40.         return value.toString();  
  41.     }  
  42.  
  43.     public String getFormat() {  
  44.         return format;  
  45.     }  
  46.  
  47.     public void setFormat(String format) {  
  48.         this.format = format;  
  49.     }  
  50.  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。