当前位置: 首页 > Java > 正文

json-lib date对象转json

关键字:
1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import net.sf.json.JSONObject;   
import net.sf.json.JsonConfig;   
import net.sf.json.processors.JsonValueProcessor;   
 
public class DateJsonValueProcessor implements JsonValueProcessor {   
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";   
    private DateFormat dateFormat;   
 
    /**    
     * 构造方法.    
     *    
     * @param datePattern 日期格式    
     */  
    public DateJsonValueProcessor(String datePattern) {   
        try {   
            dateFormat = new SimpleDateFormat(datePattern);   
        } catch (Exception ex) {   
            dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);   
        }   
    }   
 
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {   
        return process(value);   
    }   
 
    public Object processObjectValue(String key, Object value,   
            JsonConfig jsonConfig) {   
        return process(value);   
    }   
 
    private Object process(Object value) {   
        if (value == null) {   
            value = new Date();   //为null时返回当前日期,也可以返回"",看需要   
        }   
        return dateFormat.format((Date) value);   
    }   
 
    public static String map2JSonStr(Map map) {   
        JsonConfig jsonConfig = new JsonConfig();   
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(   
                DEFAULT_DATE_PATTERN);   
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);   
 
        JSONObject jsonObject = JSONObject.fromObject(map, jsonConfig);   
 
        return jsonObject.toString();   
    }      
 
    //将对象转换为json string,使用上面定义的的日期格式   
    @SuppressWarnings("unchecked")   
    public static JSONObject obj2JsonObj(Object obj) {   
        JsonConfig jsonConfig = new JsonConfig();   
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(   
                DEFAULT_DATE_PATTERN);   
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);   
 
        JSONObject jsonObject = JSONObject.fromObject(obj, jsonConfig);   
 
        return jsonObject;   
    }   
}  
 
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
 
public class DateJsonValueProcessor implements JsonValueProcessor {
	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private DateFormat dateFormat;
 
	/**  
	 * 构造方法.  
	 *  
	 * @param datePattern 日期格式  
	 */
	public DateJsonValueProcessor(String datePattern) {
		try {
			dateFormat = new SimpleDateFormat(datePattern);
		} catch (Exception ex) {
			dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		}
	}
 
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}
 
	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}
 
	private Object process(Object value) {
		if (value == null) {
			value = new Date();   //为null时返回当前日期,也可以返回"",看需要
		}
		return dateFormat.format((Date) value);
	}
 
	public static String map2JSonStr(Map map) {
		JsonConfig jsonConfig = new JsonConfig();
		DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(
				DEFAULT_DATE_PATTERN);
		jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);
 
		JSONObject jsonObject = JSONObject.fromObject(map, jsonConfig);
 
		return jsonObject.toString();
	}	
 
	//将对象转换为json string,使用上面定义的的日期格式
	@SuppressWarnings("unchecked")
	public static JSONObject obj2JsonObj(Object obj) {
		JsonConfig jsonConfig = new JsonConfig();
		DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(
				DEFAULT_DATE_PATTERN);
		jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);
 
		JSONObject jsonObject = JSONObject.fromObject(obj, jsonConfig);
 
		return jsonObject;
	}
}

本文固定链接: http://www.chepoo.com/json-lib-date-handle.html | IT技术精华网

json-lib date对象转json:等您坐沙发呢!

发表评论