1. 对象(示例为 UserEntity)转 json 数据
writeValueAsString 方法
public static void main(String[] args) throws JsonProcessingException {
UserEntity userEntity=new UserEntity();
userEntity.setId(100);
userEntity.setName(“JCccc”);
userEntity.setAge(18);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(userEntity);
System.out.println(jsonString);
}
控制台输出:
格式很漂亮,是因为使用了 :
咱们不需要漂亮,所以后面的我都不使用格式的方法了,转换的时候,只需要 writeValueAsString 就够了 。
2. json 数据 转 对象
readValue 方法
ObjectMapper mapper = new ObjectMapper();
//json字符串转对象
UserEntity userEntityNew = mapper.readValue(jsonString, UserEntity.class);
System.out.println(userEntityNew);
控制台输出:
3. map 转 json 数据
ObjectMapper mapper = new ObjectMapper();
Map map=new HashMap();
map.put(“A”,1);
map.put(“B”,2);
map.put(“C”,3);
map.put(“D”,4);
String jsonMap = mapper.writeValueAsString(map);
System.out.println(jsonMap);
控制台输出:
4. json 数据 转 map
ObjectMapper mapper = new ObjectMapper();
//json字符串转为Map对象
Map mapNew=mapper.readValue(jsonMap, Map.class);
System.out.println(mapNew);
控制台输出:
5. List 转 json 数据
ObjectMapper mapper = new ObjectMapper();
UserEntity userEntity1=new UserEntity();
userEntity1.setId(101);
userEntity1.setName(“JCccc1”);
userEntity1.setAge(18);
UserEntity userEntity2=new UserEntity();
userEntity2.setId(102);
userEntity2.setName(“JCccc2”);
userEntity2.setAge(18);
UserEntity userEntity3=new UserEntity();
userEntity3.setId(103);
userEntity3.setName(“JCccc3”);
userEntity3.setAge(18);
List userList=new ArrayList<>();
userList.add(userEntity1);
userList.add(userEntity2);
userList.add(userEntity3);
String jsonList = mapper.writeValueAsString(userList);
System.out.println(jsonList);
控制台输出:
6. json 数据 转 List
ObjectMapper mapper = new ObjectMapper();
List userListNew= mapper.readValue(jsonList,new TypeReference<List>(){});
System.out.println(userListNew.toString());
控制台输出:
7.接口接收稍微复杂一点的json数据,如何拆解
现在模拟了一串稍微复杂一些的json数据,如:
{
“msg”: “success”,
“data”: [
{
“id”: 101,
“name”: “JCccc1”,
“age”: 18
},
{
“id”: 102,
“name”: “JCccc2”,
“age”: 18
},
{
“id”: 103,
“name”: “JCccc3”,
“age”: 18
}
],
“status”: 200
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。
面试题多多少少对于你接下来所要做的事肯定有点帮助,但我更希望你能透过面试题去总结自己的不足,以提高自己核心技术竞争力。每一次面试经历都是对你技术的扫盲,面试后的复盘总结效果是极好的!
**
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-tnn2rdeu-1711991850809)]
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。
面试题多多少少对于你接下来所要做的事肯定有点帮助,但我更希望你能透过面试题去总结自己的不足,以提高自己核心技术竞争力。每一次面试经历都是对你技术的扫盲,面试后的复盘总结效果是极好的!
[外链图片转存中…(img-l6E80cm8-1711991850810)]