json工具类

阅读更多
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author zhongmin
 */
public class JsonUtil
{
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	public static String toJson(Object value)
	{
		try
		{
			return objectMapper.writeValueAsString(value);
		} catch (JsonProcessingException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	public static  T from(Object value, Class valueType)
	{
		try
		{
			return objectMapper.readValue(value.toString(), valueType);
		} catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}
}

 

你可能感兴趣的:(json)