🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**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); } ~~~