snake23

对象POJO和JSON互转

复制代码
public class JsonUtil {
    /**
     * JSON 转 POJO
     */
     public static <T> T getObject(String pojo, Class<T> tclass) {
            try {
                return JSONObject.parseObject(pojo, tclass);
            } catch (Exception e) {
                log.error(tclass + "转 JSON 失败");
            }
            return null;
     }
     
     /**
      * POJO 转 JSON    
      */
     public static <T> String getJson(T tResponse){
         String pojo = JSONObject.toJSONString(tResponse);
         return pojo;
     }
     
}
复制代码

 

List集合和JSON互转工具类

复制代码
public class JsonListUtil {
    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts);
        return jsons;
    }

    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }

}
复制代码

 

分类:

技术点:

相关文章:

  • 2021-11-18
  • 2022-02-07
  • 2022-02-21
  • 2021-12-31
  • 2022-12-23
  • 2022-02-07
  • 2021-12-29
  • 2022-01-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-12-31
相关资源
相似解决方案