jackson对象转json

299 2024-03-09 19:17

java import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJsonConverter { public static void main(String[] args) { ObjectMapper objectMapper = new ObjectMapper(); // 创建一个Java对象 MyObject myObject = new MyObject("John", 30); try { // 将Java对象转换为JSON字符串 String json = objectMapper.writeValueAsString(myObject); System.out.println(json); } catch (Exception e) { e.printStackTrace(); } } } class MyObject { private String name; private int age; public MyObject(String name, int age) { this.name = name; this.age = age; } // 省略getter和setter方法 }
顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
点击我更换图片