json串和java对象互相转换by jackson

复制代码
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class JacksonUtils {
    
    private final static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    }

    /**
     * 将对象转换成json字符串,用于将发送的报文打印出到日志
     */
    public static String toString(Object object) {

        String result = null;
        if (null == object)
            return result;
        try {
            result = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            System.out.println("对象转换成json字符串出错");
        }
        return result;
    }
    
    /**
     * 将json字符串转换成对象,用于将发送的报文打印出到日志
     * @return 
     */
    public static <T> T getObjectFromJsonString(String jsonStr, Class<T> responseType) {

        T result = null;
        if (null == jsonStr)
            return result;
        try {
         result = mapper.readValue(jsonStr, responseType);
      } catch (IOException e) {
           e.printStackTrace();
         System.out.println("json字符: " + jsonStr +" 串转换成对象出错");
      }
        return result;
    }
    
    /**
     * 从文件中读取json字符串,转换成对象列表
     * @param fileName
     * @param JsonObjectType
     * @return
     */
    public static <T> List<T> readJsonFromFile(String fileName, Class<T> JsonObjectType){
      List<T> result = new ArrayList<T>();
      try {
         //File file = new File("D:\\work\\raiyitest\\a\\a.txt");
         File file = new File(fileName);
         InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
         BufferedReader br = new BufferedReader(new FileReader(file));
         //BufferedReader br = new BufferedReader(isr);
         String data = null;
         while((data = br.readLine()) != null){
            T myRule = getObjectFromJsonString(data, JsonObjectType);
            result.add(myRule);
         }
         br.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
    

   /**
    * 将对象列表转换成json字符串,写入文件
    * @param fileName
    * @param jsonObjectList
    */
   public static <T> void WriteJsonObjectToFile(String fileName, List<T> jsonObjectList){
      try {
         //File file = new File("D:\\work\\raiyitest\\a\\a.txt");
         File file = new File(fileName);
           OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
           BufferedWriter bw = new BufferedWriter(osw);
         for(T jsonObject : jsonObjectList){
            String myJsonStr = toString(jsonObject);
            System.out.println(myJsonStr);
            bw.write(myJsonStr);
            bw.newLine();
         }
         bw.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
相关推荐
Hui Baby3 分钟前
java抠图
java·开发语言
许彰午18 分钟前
# 手写一个迷你Tomcat——三步理解Servlet容器的核心原理
java·servlet·tomcat
CyrusCJA34 分钟前
在Windows系统上将Redis注册为系统服务使其实现开机自启
数据库·windows·redis·缓存
一诺加油鸭1 小时前
若依后端系统集成 Swagger 接口文档功能
java·开发语言
云烟成雨TD2 小时前
Spring AI Alibaba 1.x 系列【40】多智能体核心模式 - 智能体作为工具(Agent as Tool)
java·人工智能·spring
测试员周周2 小时前
【踩坑系列3】飞书机器人集体“失联“?3 个 Gateway 进程让我差点崩溃!一个测试老兵的排查实录
java·python
aq55356002 小时前
Laravel9.x新特性全解析
java·开发语言·数据库
亦暖筑序2 小时前
AI 客服系统升级实战:多 Agent 路由 + 多轮记忆 + 敏感词过滤
java·后端
zhangzeyuaaa2 小时前
深入理解 Python GIL:从机制到释放时机
java·网络·python
烤麻辣烫2 小时前
json与fastjson
前端·javascript·学习·json