皮皮虾的倔强 于 2022-05-23 13:25:09 发布 13531 收藏 57 文章标签: java json 开发语言 版权 一、准备工作 定义三个json字符串用于以下测试: //json字符串-简单对象 String jsonStr = "{\"studentName\":\"张三\",\"studentAge\":18}"; //json字符串-数组类型 String jsonArrStr = "[{\"studentName\":\"张三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":17}]"; //json字符串-复杂对象 String complexJsonStr= "{\"teacherName\":\"李寻欢\",\"teacherAge\":30,\"course\":{\"courseName\":\"武术\",\"code\":110}, \"students\":[{\"studentName\":\"张三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":19}]}"; 1 2 3 4 5 6 7 二、json字符串、json对象、java对象的转换方法 1.JSON字符串到JSON对象的转换 (1)json字符串-简单对象与JSONObject之间的转换 JSONObject jsonObj = JSON.parseObject(jsonStr); 1 (2)json字符串-数组类型与JSONArray之间的转换 JSONArray jsonArray = JSON.parseArray(jsonArrStr); //遍历JSONArray方法1 for(int i = 0; i < jsonArray.size(); i++){ JSONObject jsonObj = jsonArray.getJSONObject(i); } //遍历JSONArray方法2 for(Object obj : jsonArray){ JSONObject jsonObject = (JSONObject) obj; } 1 2 3 4 5 6 7 8 9 (3)json字符串-复杂对象与JSONObject之间的转换 JSONObject jsonObj = JSON.parseObject(complexJsonStr); //取出复杂对象中各项内容 String teacherName = jsonObj.getString("teacherName"); Integer teacherAge = jsonObj.getInteger("teacherAge"); JSONObject course = jsonObj.getJSONObject("course"); JSONArray students = jsonObj.getJSONArray("students"); 1 2 3 4 5 6 2.JSON对象到JSON字符串的转换 JSONObject jsonObj = new JSONObject(); //JSONObject到JSON字符串的转换 String jsonStr = jsonObj.toJSONString(); 1 2 3 3.JSON字符串到Java对象的转换 JSON字符串与JavaBean之间的转换建议使用TypeReference类 (1)json字符串-简单对象与Java对象之间的转换 // 方法1 Student student = JSON.parseObject(jsonStr , new TypeReference() {}); // 方法2 Student student = JSON.parseObject(jsonStr , Student.class); 1 2 3 4 (2)json字符串-数组与Java对象之间的转换 ArrayListstudents = JSON.parseObject(jsonArrStr, new TypeReference() {}); for (Student student : students) { System.out.println(student.getStudentName()+":"+student.getStudentAge()); } 1 2 3 4 (3)json字符串-复杂对象与Java对象之间的转换 Teacher teacher = JSON.parseObject(complexJsonStr, new TypeReference() {}); //获取teacher中的内容 String teacherName = teacher.getTeacherName(); Integer teacherAge = teacher.getTeacherAge(); Course course = teacher.getCourse(); Liststudents = teacher.getStudents(); 1 2 3 4 5 6 4.Java对象到JSON字符串的转换 Teacher teacher = new Teacher(); String jsonStr = JSON.toJSONString(teacher); 1 2 5.Java对象到JSON对象的转换 String jsonStr= JSON.toJSONString(student); JSONObject jsonObj = JSON.parseObject(jsonStr); 1 2 6.JSON对象到Java对象的转换 # 方法1,先转换为json字符串,再使用parseObject String jsonStr = jsonObj.toJSONString(); Student stu = JSON.parseObject(jsonStr,new TypeReference() {}); # 方法2,直接使用toJavaObject Student stu = JSON.toJavaObject(jsonObj, Student.class); 1 2 3 4 5 例子 1.json字符串转化为java实体类 (parseObject) ApprovalVo approvalVo = JSON.parseObject(str, ApprovalVo.class); // str == json字符串 // ApprovalVo == 实体类 1 2 3 2.json字符串转化为list对象 (parseArray) String str2 = "[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]"; Listusers = JSON.parseArray(jsonStr2, User.class); 1 2 3.json字符串转化为复杂java对象 (parseObject) // 复杂对象->>>>对象中嵌套对象的 String str3 = "{'name':'userGroup','users':[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]}"; UserGroup userGroup = JSON.parseObject(jsonStr3, UserGroup.class); 1 2 3 把实体类转化成json字符串 String str = JSON.toJSONString(ApprovalVo); 1 把json字符串转化成JSONObject String jsonStr = "{\"school\":\"商职\",\"sex\":\"男\",\"name\":\"wjw\",\"age\":22}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject.getString("name")); System.out.println(jsonObject.getInteger("age")); 1 2 3 4 参考: https://zhuanlan.zhihu.com/p/476747417 https://blog.51cto.com/u_15281317/3008066 ———————————————— 版权声明:本文为CSDN博主「皮皮虾的倔强」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Wolves_howl/article/details/124921713 |
|