JacksonUtilTest

public class JacksonUtilTest { public static void main(String\[\] args) { // 你的原始 JSON 字符串 String jsonStr = "{" + " \"clsAttrs\": " + " {" + " \\"productionCatlogLevel\\": {" + " \\"name\\": \\"3333333\\"," + " \\"brandInternationalName\\": \\"2222\\"," + " \\"states\\": {" + " \\"code\\": \\"停用\\"," + " \\"cnName\\": \\"停用\\"" + " }" + " }" + " }" + " " + "}"; // 示例1:解析 JsonNode 并获取 name(你的核心需求) JsonNode jsonNode = JacksonUtil.toJsonNode(jsonStr); String name = JacksonUtil.getStringValue(jsonNode, "clsAttrs0.productionCatlogLevel.name"); System.out.println("获取的name值:" + name); // 输出:3333333 // 示例2:获取嵌套的 states.cnName String cnName = JacksonUtil.getStringValue(jsonNode, "clsAttrs0.productionCatlogLevel.states.cnName"); System.out.println("获取的cnName值:" + cnName); // 输出:停用 // 示例3:对象转 JSON(格式化输出) User user = new User("张三", 25); String userJson = JacksonUtil.toJson(user, true); System.out.println("对象转JSON:\n" + userJson); // 示例4:JSON 转复杂类型(List<Map>) String listJson = "{\\"name\\":\\"李四\\",\\"age\\":30},{\\"name\\":\\"王五\\",\\"age\\":35}"; List<Map<String, Object>> list = JacksonUtil.fromJson(listJson, new TypeReference<List<Map<String, Object>>>() {}); System.out.println("JSON转List:" + list); } // 测试用实体类 static class User { private String name; private Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } // 省略 getter/setter(Jackson 序列化需要) public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } }

相关推荐
独隅8 小时前
IntelliJ IDEA 在 Windows 上的完整安装与使用指南
java·windows·intellij-idea
逻极9 小时前
Windows 平台 Ollama AMD GPU 一键编译指南:基于 ROCm 7.1 的自动化实战
人工智能·windows·stm32·自动化·gpu·amd·ollama
zhangfeng11339 小时前
JupyterLab 里,JSON文件纯文本格式编辑 / 查看
人工智能·json
caimouse10 小时前
Reactos 第 9 章 设备驱动 — 9.13 同步I/O与异步I/O
windows
一个被程序员耽误的厨师10 小时前
04-实践篇-让AI生成可视化页面-ai-json-ui的落地实践
人工智能·ui·json
caimouse10 小时前
Reactos 第 9 章 设备驱动 — 9.10 磁盘的Miniport驱动模块
windows·嵌入式硬件
caimouse10 小时前
Reactos 第 9 章 设备驱动 — 9.11 命名管道与Mailslot
windows
x***r15110 小时前
Krita 5.2.13 安装教程 Windows版:自定义路径+开源绘画软件配置指南
windows
就改了11 小时前
Windows Elasticsearch 完整上手教程
大数据·windows·elasticsearch
fastjson_11 小时前
备份与恢复驱动
windows