ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的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); } ~~~