JsonUtil

阅读更多
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
	
	private static final Logger log = Logger.getLogger(JsonUtils.class);
	
	private final static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * get Instance of ObjectMapper
	 * 
	 * @return ObjectMapper
	 */
	public static ObjectMapper getInstance() {
		return objectMapper;
	}

	/**
	 * JavaBean convert to Json
	 * @param bean
	 * @return
	 */
	public static String bean2Json(Object bean) {
		try {
			objectMapper.setSerializationInclusion(Include.NON_EMPTY);
			return objectMapper.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			log.error("jackson进程异常:" + e.getMessage());
			return null;
		}
	}

	/**
	 * Json convert to JavaBean
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static  T json2Bean(String json, Class clazz) {
		try {
			return objectMapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			log.error("Jackson解析异常:" + e.getMessage());
			return null;
		} catch (JsonMappingException e) {
			log.error("Jackson映射异常:" + e.getMessage());
			return null;
		} catch (IOException e) {
			log.error("IO异常:" + e.getMessage());
			return null;
		}
	}

	/**
	 * Json convert to Map
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  Map json2Map(String json) {
		try {
			return objectMapper.readValue(json, Map.class);
		} catch (JsonParseException e) {
			log.error("Jackson解析异常:" + e.getMessage());
			return null;
		} catch (JsonMappingException e) {
			log.error("Jackson映射异常:" + e.getMessage());
			return null;
		} catch (IOException e) {
			log.error("IO异常:" + e.getMessage());
			return null;
		}
	}

	/**
	 * Json convert to Map with javaBean
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static  Map json2Map(String json, Class clazz) {
		Map> map;
		try {
			map = objectMapper.readValue(json, new TypeReference>(){});
			Map result = new HashMap();
			for (Entry> entry : map.entrySet()) {
				result.put(entry.getKey(), map2Bean(entry.getValue(), clazz));
			}
			return result;
		} catch (JsonParseException e) {
			log.error("Jackson解析异常:" + e.getMessage());
			return null;
		} catch (JsonMappingException e) {
			log.error("Jackson映射异常:" + e.getMessage());
			return null;
		} catch (IOException e) {
			log.error("IO异常:" + e.getMessage());
			return null;
		}
	}

	/**
	 * Json convert to List with JavaBean
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static  List json2List(String json, Class clazz) {
		List> list;
		try {
			list = objectMapper.readValue(json, new TypeReference>() {});
			List result = new ArrayList();
			for (Map map : list) {
				result.add(map2Bean(map, clazz));
			}
			return result;
		} catch (JsonParseException e) {
			log.error("Jackson解析异常:" + e.getMessage());
			return null;
		} catch (JsonMappingException e) {
			log.error("Jackson映射异常:" + e.getMessage());
			return null;
		} catch (IOException e) {
			log.error("IO异常:" + e.getMessage());
			return null;
		}
	}

	/**
	 * Map convert to JavaBean
	 * 
	 * @param map
	 * @param clazz
	 * @return bean
	 */
	@SuppressWarnings("rawtypes")
	public static  T map2Bean(Map map, Class clazz) {
		return objectMapper.convertValue(map, clazz);
	}
}

你可能感兴趣的:(json,java,工具类)