import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.commons.lang.StringUtils;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.codehaus.jackson.type.TypeReference;public class DataConverter { /** * map 转变成bean * @param t * @param map * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public staticT map2Bean(T t,Map map) { T entity=null; try { Class clazz = t.getClass(); entity = (T)clazz.newInstance(); Set keys = map.keySet(); for(String key:keys){ String fieldName = key; if(key.equals(key.toUpperCase())){ Field[] fields = clazz.getDeclaredFields(); for(Field field: fields){ if(field.getName().toUpperCase().equals(key)) fieldName=field.getName(); break; } } try { Class paramClass = clazz.getDeclaredField(fieldName).getType(); String methodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); Method method = clazz.getMethod(methodName, paramClass); method.invoke(entity, map.get(key)); } catch (Exception e) { } } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return entity; } /** * json转map * @param jsonStr * @return */ public static Map jsonToMap(String jsonStr){ if(StringUtils.isBlank(jsonStr)){ return null; } ObjectMapper mapper = null; try { mapper = new ObjectMapper(); return mapper.readValue(jsonStr,new TypeReference
import java.io.IOException;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.DeserializationConfig;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.codehaus.jackson.map.ser.FilterProvider;import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;import org.codehaus.jackson.type.TypeReference;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.StringUtils;public class JsonUtil { private static Logger logger = LoggerFactory.getLogger(JsonUtil.class) ; private static ObjectMapper mapper = new ObjectMapper(); //must reuse!! static{// java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// mapper.getDeserializationConfig().withDateFormat(df); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public static String serializeToJson(Object obj, String... ignoreProperties) {// mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); FilterProvider filterProvider = new SimpleFilterProvider().addFilter( "ignorePropertiesFilter", SimpleBeanPropertyFilter.serializeAllExcept(ignoreProperties)); String json; try { json = mapper.writer(filterProvider).writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException(e); } return json; } public staticT unserializeFromJson(String json, Class clazz) { try { T object=null; if(StringUtils.hasText(json)){ object = mapper.readValue(json, clazz); } return object; } catch (Exception e) { logger.error("can not serialize:"+json); e.printStackTrace(); throw new RuntimeException(e); } } public static T unserializeFromJson(String json, Type type) { try { T object=null; if(StringUtils.hasText(json)){ object = mapper.readValue(json, mapper.getTypeFactory().constructType(type)); } return object; } catch (Exception e) { logger.error("can not serialize:"+json); e.printStackTrace(); throw new RuntimeException(e); } } public static List unserializeFromJsonAsList(String json, Class cls) { try { List object = mapper.readValue(json, mapper.getTypeFactory() .constructCollectionType(ArrayList.class, cls)); return object; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static T unserializeFromJsonByTypeReference(String json, TypeReference typeReference) throws JsonParseException, JsonMappingException, IOException { T object = mapper.readValue(json, typeReference); return object; } public static T convertValue(Object fromValue, Class toValueType){ return mapper.convertValue(fromValue, toValueType); }}