JSON toBean Timestamp To Date 时间戳转日期

JSON的日期格式转换,当遇到日期格式为Timestamp 时如:1376496000000默认情况下是无法转换为Date类型的加入一下方法便个转换:

import java.util.Date;

import net.sf.ezmorph.object.AbstractObjectMorpher;
/**
 * 
 * @author chaico
 *
 */
public class TimestampToDateMorpher extends AbstractObjectMorpher {
	

   

    public Object morph(Object value) {
    	if( value != null){
    		return new Date(Long.parseLong(String.valueOf(value)));
    	} 	
    	return null;
    }

    @Override
    public Class morphsTo() {
    	return Date.class;
    }
    
    public boolean supports( Class clazz ){
       return Long.class.isAssignableFrom( clazz );
    }
}

 在JSON toBean时调用:

JSONUtils.getMorpherRegistry().registerMorpher(new TimestampToDateMorpher());
			JSONObject jsonObject = JSONObject.fromObject(responseJson);
			JSONObject.toBean(jsonObject, this.getClass());

 

你可能感兴趣的:(Json toBean)