import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @author: HuYi.Zhang
* @create: 2018-04-24 17:20
**/
public class JsonUtils {
public static final ObjectMapper mapper = new ObjectMapper();
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
/**
* 转换成json
* @param obj
* @return
*/
@Nullable
public static String serialize(Object obj) {
if (obj == null) {
return null;
}
if (obj.getClass() == String.class) {
return (String) obj;
}
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
logger.error("json序列化出错:" + obj, e);
return null;
}
}
@Nullable
public static <T> T parse(String json, Class<T> tClass) {
try {
return mapper.readValue(json, tClass);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
@Nullable
public static <E> List<E> parseList(String json, Class<E> eClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
@Nullable
public static <K, V> Map<K, V> parseMap(String json, Class<K> kClass, Class<V> vClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
@Nullable
public static <T> T nativeRead(String json, TypeReference<T> type) {
try {
return mapper.readValue(json, type);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
static class Usert {
String name;
Integer age;
}
public static void main(String[] args) {
Usert usert = new Usert("zhangshang", 343);
usert.setAge(23);
usert.setName("zhagns");
usert.setName("qingchu");
usert.setAge(233);
String json = serialize(usert);
System.out.println("json输出:"+json);
Usert parse = parse(json, Usert.class);
System.out.println("tostring 输出:"+parse);
/* String json = "[2,3,43,54,45,34]";
List<Integer> list = parseList(json, Integer.class);
list.stream().forEach(n-> System.out.print(n+" "));
System.out.println(list);*/
/* Map<String, String> map = parseMap(json, String.class, String.class);
System.out.println("map-->"+map);*/
String newlist = "[{\"name\":\"qingchu\",\"age\":233},{\"name\":\"guxiang\",\"age\":54},{\"name\":\"beijing\",\"age\":23553}]";
List<Map<String, String>> maps = nativeRead(newlist, new TypeReference<List<Map<String, String>>>() {});
for (Map<String, String> map : maps) {
System.out.println("new_map::"+map);
}
}
JsonUtils工具类学习记录
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Git 学习记录 03
计算机新手的 git 学习记录
git 文件编辑器 版本控制系统 -
JsonUtils 工具类
JsonUtils 工具类
JsonUtils 工具类 -
JsonUtils工具类和Jsonp发起http请求
前言Jsonp用来调用http请求的,非常小巧而且方便回顾之前所学二者依赖如
Json json java json字符串 -
java工具类记录
java中,日常开发使用工具类的记录
日期类 java 工具类 -
【微服务】167:工具类JsonUtils完成json的序列化和反序列化
自学Java第167天
java 微服务 json json字符串 反序列化