💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**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); } ~~~