使用 Jackson 解析 JSON、ObjectMapper解析 JSON、ObjectMapper实现对象转json、Jackson 实现对象转json

工作中经常需要对象转json,json转对象来实现数据的接受或发送,下面是个人使用的工具类:

import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.core.JsonEncoding;

import com.fasterxml.jackson.core.JsonFactory;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationFeature;

import com.fasterxml.jackson.databind.MapperFeature;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializationFeature;

import com.fasterxml.jackson.databind.node.ObjectNode;

import com.wanmagroup.haikang.constants.Constants;

import org.springframework.http.*;

import org.springframework.web.client.RestTemplate;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Map;

public class JsonUtils {

public static String ctreteJsonString(String key, Object obj) {

try {

// 使用封装的 ObjectMapper 创建 JSON 对象

ObjectMapper objectMapper = createObjectMapper();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

objectMapper.setDateFormat(dateFormat);

// 使用封装的 JSON 工厂创建 JSON 生成器

JsonFactory jsonFactory = createJsonFactory();

JsonGenerator jsonGenerator = jsonFactory.createGenerator(System.out, JsonEncoding.UTF8);

// 创建一个 JSON 对象节点

ObjectNode rootNode = createJsonNode(key, obj);

// 使用 JSON 生成器将 JSON 对象写入输出流

objectMapper.writeTree(jsonGenerator, rootNode);

// 完成后关闭生成器

jsonGenerator.close();

// 将 JSON 对象转换为字符串

String userInfoJson = objectMapper.writeValueAsString(rootNode);

System.out.println(userInfoJson);

return userInfoJson;

} catch (IOException e) {

// 处理异常,可以打印错误日志或者采取其他必要的操作

e.printStackTrace();

return null; // 或者返回适当的错误信息

}

}

public static ObjectMapper createObjectMapper() {

ObjectMapper objectMapper = new ObjectMapper();

// 如果需要排除空字段

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

// 如果需要排除空字段

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

return objectMapper;

}

public static ObjectMapper createObjectMapperAll() {

ObjectMapper objectMapper = new ObjectMapper();

// 如果需要排除空字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 如果需要排除空字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//configure() 方法忽略掉"无法识别"的字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略值为默认值的属性
objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
// 接受不区分大小写的属性
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

return objectMapper;

}

// 封装 JSON 工厂配置的方法

public static JsonFactory createJsonFactory() {

JsonFactory jsonFactory = new JsonFactory();

jsonFactory.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);

return jsonFactory;

}

// 创建包含指定键的 JSON 对象节点

public static <T> ObjectNode createJsonNode(String key, T object) {

ObjectMapper objectMapper = createObjectMapper();

ObjectNode rootNode = objectMapper.createObjectNode();

rootNode.putPOJO(key, object);

return rootNode;

}

//map转spring,组成请求体拼接url即可发送

public static String jsonRequest(Map<String,Object>map){

ObjectMapper objectMapper =JsonUtils.createObjectMapper();

String jsonRequest = "";

try {

jsonRequest = objectMapper.writeValueAsString(map);

} catch (JsonProcessingException e) {

e.printStackTrace();

}

System.out.println(jsonRequest);

return jsonRequest;

}

}


相关推荐
制造数据与AI践行者老蒋4 小时前
离谱!PromptTemplate 遇上 JSON,花括号直接引发解析战争
数据库·microsoft·json
chenjingming66611 小时前
JWT(JSON Web Token)有效期查看登录过期时间
json
绘梨衣的sakura路1 天前
Fastjson ≤ 1.2.83 新型 RCE 漏洞深度分析:三层绕过机制与完整利用链
安全·web安全·json
江湖十年1 天前
Go 语言中 YAML to JSON 踩坑笔记
后端·go·json
前网易架构师-高司机2 天前
带标注的扑克牌识别数据集,识别率99.5%,3083张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·数字·扑克牌·纸牌
前网易架构师-高司机2 天前
带标注的浮游藻类24种数据集,识别率91.5 %数据集, 23175张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·json·数据集·微观·生物·浮游·藻类
维天说2 天前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
一颗小行星-4 天前
为什么有的JSON可以带注释?
json
CedarQR4 天前
万字长文:从零在 RK3588 上部署 PaddleSpeech 中文 TTS 全流程(FastSpeech2 + HiFiGAN)
开发语言·c++·嵌入式硬件·ubuntu·json
Geoking.5 天前
JSON vs JSONL:从数据格式到 AI Agent 的工程实践
人工智能·深度学习·json