码迷,mamicode.com
首页 > 编程语言 > 详细

java的json序列化

时间:2016-07-06 19:59:26      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 序列化帮助类
 */
public class SerializeHelper {

    private static ObjectMapper objectMapper = new ObjectMapper();  
    
    /** 
     * 将对象序列化为JSON字符串 
     *  
     * @param object 
     * @return JSON字符串 
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonGenerationException 
     */  
    public String serialize(Object object) throws JsonGenerationException, JsonMappingException, IOException {  
        Writer write = new StringWriter();  
        objectMapper.writeValue(write, object);  
        return write.toString();  
    }  
  
    /** 
     * 将JSON字符串反序列化为对象 
     *  
     * @param object 
     * @return JSON字符串 
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     */  
    public <T> T deserialize (String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {  
        Object object = objectMapper.readValue(json, TypeFactory.rawClass(clazz));  
        return (T) object;  
    }  
	
}

java的json序列化

标签:

原文地址:http://www.cnblogs.com/xinziyublog/p/5647812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!