1.org中jackson转换json,springboot中内置jackson
ObjectMapper o=new ObjectMapper();
List<>list=new ArrayList();
String jons=o.writeAsValueString(list);
2.alibaba中fastjson转换成json
@GetMapping("/test")public TbUser testHttpClient(){String url="http://localhost:8080/saveUser";TbUser user = new TbUser();user.setPhone("13568775074");user.setUsername("minming");user.setPassword("123456");//fastjson的JSONObject将Object转jsonString s = JSONObject.toJSONString(user);//返回json类型数据String result = CloseableHttpCilentsUtils.doPost(url, user, null);fastjson的JSONObject将json转ObjectJSONObject response = JSONObject.parseObject(result);TbUser parse = JSONObject.toJavaObject(response, TbUser.class);return parse;}
返回结果:
{"password": "123456","phone": "13568775074","username": "minming"
}