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();
      }
   }
}
相关推荐
武子康6 分钟前
Java-200 RabbitMQ 架构与 Exchange 路由:fanout/direct/topic/headers
java·架构·消息队列·系统架构·rabbitmq·java-rabbitmq·mq
渡我白衣7 分钟前
C++可变参数队列与压栈顺序:从模板语法到汇编调用约定的深度解析
c语言·汇编·c++·人工智能·windows·深度学习·硬件架构
计算机学姐7 分钟前
基于SSM的社区外来务工人员管理系统【2026最新】
java·vue.js·java-ee·tomcat·maven·intellij-idea·mybatis
ysy16480672398 分钟前
RabbbitMQ入门:从Windows版本RabbitMQ安装到Spring AMQP实战(一)
windows·rabbitmq·java-rabbitmq
好学且牛逼的马9 分钟前
HttpServlet 深度拆解:从设计模式看透其核心原理
java·servlet·设计模式
顾安r9 分钟前
12.17 脚本网页 创意导航
java·linux·前端·游戏·html
Json____9 分钟前
springboot框架对接物联网,配置TCP协议依赖,与设备通信,让TCP变的如此简单
java·spring boot·后端·tcp/ip
洛阳泰山10 分钟前
快速上手 MaxKB4J:开源企业级智能知识库系统在 Sealos 上的完整部署指南
java·开源·llm·agent·rag
risc12345611 分钟前
【Elasticsearch】副本恢复机制文件级(file-based)操作级(ops-based)顶级理解
java·mysql·lucene
后端小张11 分钟前
【JAVA 进阶】深入拆解SpringBoot自动配置:从原理到实战的完整指南
java·开发语言·spring boot·后端·spring·spring cloud·springboot