json和json对象的相互转换
Java对象转为JSON数据
步骤:
导入 Jackson 相关 jar 包
创建 Jackson 核心对象 ObjectMapper
调用 ObjectMapper 的相关方法进行转换
转换方法:
writeValue(参数,obj)
参数为File:将obj 对象转换为Json字符串,并保存到指定的文件中
参数为 Writer:将obj对象转换为Json字符串,并将 json 数据填充到字符输出流中
参数为 OutputStream:将 obj 对选哪个转换为Json字符串,并将json 数据填充到字节输出流中
writeValueAsString(obj):将对象转换为json字符串
注解:在属性前面加注解
@JsonIgnore:排除属性
@JsonFormat:属性值的格式化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | // Java对象转为JSON字符串(反序列化操作) @Test public void test1() throws Exception{ //1. 创建Person对象 Person p = new Person(); p.setName( "朝阳" ); p.setAge(22); p.setGender( "man" ); //2. 创建Jackson的核心对象, ObjectMapper ObjectMapper mapper = new ObjectMapper(); //3. 转换 String json = mapper.writeValueAsString(p); // System.out.println(json); // {"name":"朝阳","age":22,"gender":"man"} // writeValue(File, obj) 将数据写入到e://a.txt文件中 // mapper.writeValue(new File("e://a.txt"), p); //writeValue.将数据关联到Writer中 //mapper.writeValue(new FileWriter("e://b.txt"),p); } @Test public void test2() throws Exception{ Person p = new Person(); p.setName( "张三" ); p.setAge(22); p.setGender( "男" ); p.setBirthday( new Date()); /** * // @JsonIgnore //忽略该属性 * @JsonFormat(pattern = "yyyy-MM-dd") // 属性值格式化 * private Date birthday; */ // 转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(p); System. out .println(json); //{"name":"张三","age":22,"gender":"男","birthday":1581061966873} } @Test public void test3() throws Exception{ //1.创建Person对象 Person p = new Person(); p.setName( "张三" ); p.setAge(23); p.setGender( "男" ); p.setBirthday( new Date()); Person p1 = new Person(); p1.setName( "张三" ); p1.setAge(23); p1.setGender( "男" ); p1.setBirthday( new Date()); Person p2 = new Person(); p2.setName( "张三" ); p2.setAge(23); p2.setGender( "男" ); p2.setBirthday( new Date()); // 创建List集合 List<Person> ps = new ArrayList<>(); ps.add(p); ps.add(p1); ps.add(p2); // 转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(ps); System. out .println(json); } @Test public void test4() throws Exception { Map<String, Object> map = new HashMap<>(); map.put( "name" , "张三" ); map.put( "age" , 23); map.put( "gender" , "男" ); // 转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(map); System. out .println(json); } |
JSON数据转Java对象
步骤:
- 导入 Jackson 相关 jar 包
- 创建 Jackson 核心对象 ObjectMapper
- 调用 ObjectMapper 的相关方法进行转换
转换方法:
readValue(json字符串数据,Class)
1 2 3 4 5 6 7 8 9 10 11 12 13 | // JSON字符串转换为Java对象(序列化操作) @Test public void test5() throws Exception{ // 初始化JSON字符串 String json = "{\"name\":\"张三\",\"age\":22,\"gender\":\"男\",\"birthday\":1581061966873}" ; // 创建ObjectMapper对象 ObjectMapper mapper = new ObjectMapper(); // 转换为Java对象(Person) Person p = mapper.readValue(json, Person. class ); System. out .println(p); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端