ObjectMapper⼯具类(json转ListT)⼀ json转实体类\实体类转json\json转List<T>
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.pe.CollectionType;
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
static {
//忽略属性为null
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//忽略多余属性
}
public static String objectToString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
// TODO: handle exception
}
object toreturn null;
}
public static <T> T stringToObject(String json,Class<T> object) throws IOException {
adValue(json,object);
}
public static <T> List<T> stringToList(String json,Class<T> object){
try {
CollectionType listType = TypeFactory().constructCollectionType(ArrayList.class, object);
List<T> list = adValue(json, listType);
return list;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论