企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
**jackson ** ~~~ /** * 对象转json字符串 * @throws JsonProcessingException */ @Test public void fun1() throws JsonProcessingException { User user = new User(); user.setId(1); user.setName("nobb"); user.setPassword("111"); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writeValueAsString(user); System.out.println(s); } /** * json字符串转对象 */ @Test public void fun2(){ String objectStr = "{\"id\":1,\"name\":\"nobb\",\"password\":\"111\",\"mobile\":null}"; String arrStr = "{\"id\":1,\"name\":\"nobb\",\"password\":\"111\",\"mobile\":null}"; ObjectMapper objectMapper = new ObjectMapper(); User user = null; try { user = objectMapper.readValue(objectStr, User.class); } catch (IOException e) { e.printStackTrace(); } System.out.println(user); } ~~~