1.org中jackson转换json,springboot中内置jackson
ObjectMapper o=new ObjectMapper();
List<>list=new ArrayList();
String jons=o.writeAsValueString(list);
2.alibaba中fastjson转换成json
java
@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转json
String s = JSONObject.toJSONString(user);
//返回json类型数据
String result = CloseableHttpCilentsUtils.doPost(url, user, null);
fastjson的JSONObject将json转Object
JSONObject response = JSONObject.parseObject(result);
TbUser parse = JSONObject.toJavaObject(response, TbUser.class);
return parse;
}
返回结果:
java
{
"password": "123456",
"phone": "13568775074",
"username": "minming"
}