当前位置:   article > 正文

object对象转Json objectToJson json转pojo jsonToPojo json转集合 jsonToList

objecttojson
  1. package com.zuma.order.util;
  2. import java.util.List;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.JavaType;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. public class JsonUtils {
  7. private static final ObjectMapper MAPPER = new ObjectMapper();
  8. public static String objectToJson(Object data) {
  9. try {
  10. String string = MAPPER.writeValueAsString(data);
  11. return string;
  12. } catch (JsonProcessingException e) {
  13. e.printStackTrace();
  14. }
  15. return null;
  16. }
  17. public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
  18. try {
  19. T t = MAPPER.readValue(jsonData, beanType);
  20. return t;
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. return null;
  25. }
  26. public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
  27. JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
  28. try {
  29. List<T> list = MAPPER.readValue(jsonData, javaType);
  30. return list;
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return null;
  35. }
  36. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/950622
推荐阅读
相关标签
  

闽ICP备14008679号