`

JSONObject转换JSON--将Date转换为指定格式

    博客分类:
  • Java
阅读更多

项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的时间对象,这时JSONObject默认会将Date属性转换成这样的格式:

 

{"nanos":0,"time":-27076233600000,"minutes":0,"seconds":0,"hours":0,"month":11,"timezoneOffset":-480,"year":-789,"day":5,"date":22}

 而这种格式肯定是非常难以理解的,为了将Date转换为我们认识的“yyyy-MM-dd”格式,需要做以下操作。

 

 

首先创建一个时间转换器

 

public class JsonDateValueProcessor implements JsonValueProcessor {
	private String format ="yyyy-MM-dd";
	
	public JsonDateValueProcessor() {
		super();
	}
	
	public JsonDateValueProcessor(String format) {
		super();
		this.format = format;
	}

	@Override
	public Object processArrayValue(Object paramObject,
			JsonConfig paramJsonConfig) {
		return process(paramObject);
	}

	@Override
	public Object processObjectValue(String paramString, Object paramObject,
			JsonConfig paramJsonConfig) {
		return process(paramObject);
	}
	
	
	private Object process(Object value){
        if(value instanceof Date){  
            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);  
            return sdf.format(value);
        }  
        return value == null ? "" : value.toString();  
    }

}

 

 

然后在调用JSONObject之前创建一个JsonConfig,并且将上一步定义的date转换器注册进去:

JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());

 

最后将JsonConfig放入JSONObject对象中,这里针对不同的数据类型有多种方式放入JsonConfig:

JSONObject json = new JSONObject();
//Map转JSON
json.putAll(Map, jsonConfig);

 或者

JSONObject json = new JSONObject();
//JavaBean转JSON
json.fromObject(object, jsonConfig)

 

最后我们看到的结果就是只要JavaBean中有Date对象,转换成JSON字符串时就会变成“yyyy-MM-dd”格式。

 

3
0
分享到:
评论
11 楼 masuweng 2017-07-25  
我的就是这么弄得,到了页面还是那个格式的 。
10 楼 masuweng 2017-07-25  
    
9 楼 zcbcba123 2015-09-28  
很好用! 问题解决!O(∩_∩)O谢谢
8 楼 koreyoshi 2014-12-16  
{time=1418376181000, minutes=23, seconds=1, hours=17, month=11, year=114, timezoneOffset=-480, day=5, date=12}  这种格式转换不了呢,能指导下吗
7 楼 spp_1987 2014-12-05  
我想时间带 小时分钟秒数,结果 我在辅助类里面 更改了 格式化字符串格式,可是得到的还只是到天。
6 楼 spp_1987 2014-12-03  
我从数据库查询得到list 对象,里面包含 时间类型字符串,可是格式化成json格式,还是出现问题。 如 出现上的格式 。
5 楼 spp_1987 2014-12-03  
这里 有格式化 yyyy-mm-dd 不能 实现格式化到分钟那块 ,或者date里面有什么 就格式化到哪里?
4 楼 白糖_ 2014-09-09  
jingjdongdheaven 写道
jingjdongdheaven 写道
json.fromObject(object, jsonConfig) 
我走到这块儿老报错呢。

严重: Servlet.service() for servlet DispatcherJsonServlet threw exception
java.lang.NoSuchMethodError: net.sf.json.JSONObject.fromObject(Ljava/lang/Object;Lnet/sf/json/JsonConfig;)Lnet/sf/json/JSONObject;

确认你的object对象参数是一个JavaBean,我看你调用json.fromObject(object, jsonConfig) 中的object似乎是一个lang类型
3 楼 jingjdongdheaven 2014-09-09  
jingjdongdheaven 写道
json.fromObject(object, jsonConfig) 
我走到这块儿老报错呢。

严重: Servlet.service() for servlet DispatcherJsonServlet threw exception
java.lang.NoSuchMethodError: net.sf.json.JSONObject.fromObject(Ljava/lang/Object;Lnet/sf/json/JsonConfig;)Lnet/sf/json/JSONObject;
2 楼 jingjdongdheaven 2014-09-09  
json.fromObject(object, jsonConfig) 
我走到这块儿老报错呢。
1 楼 小龙哥 2014-02-19  
很实用的技巧!

相关推荐

Global site tag (gtag.js) - Google Analytics