|
皮皮蝦的倔強 于 2022-05-23 13:25:09 發(fā)布 13531 收藏 57 文章標簽: java json 開發(fā)語言 版權(quán) 一、準備工作 定義三個json字符串用于以下測試: //json字符串-簡單對象 String jsonStr = "{\"studentName\":\"張三\",\"studentAge\":18}"; //json字符串-數(shù)組類型 String jsonArrStr = "[{\"studentName\":\"張三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":17}]"; //json字符串-復雜對象 String complexJsonStr= "{\"teacherName\":\"李尋歡\",\"teacherAge\":30,\"course\":{\"courseName\":\"武術(shù)\",\"code\":110}, \"students\":[{\"studentName\":\"張三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":19}]}"; 1 2 3 4 5 6 7 二、json字符串、json對象、java對象的轉(zhuǎn)換方法 1.JSON字符串到JSON對象的轉(zhuǎn)換 (1)json字符串-簡單對象與JSONObject之間的轉(zhuǎn)換 JSONObject jsonObj = JSON.parseObject(jsonStr); 1 (2)json字符串-數(shù)組類型與JSONArray之間的轉(zhuǎn)換 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之間的轉(zhuǎn)換 JSONObject jsonObj = JSON.parseObject(complexJsonStr); //取出復雜對象中各項內(nèi)容 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字符串的轉(zhuǎn)換 JSONObject jsonObj = new JSONObject(); //JSONObject到JSON字符串的轉(zhuǎn)換 String jsonStr = jsonObj.toJSONString(); 1 2 3 3.JSON字符串到Java對象的轉(zhuǎn)換 JSON字符串與JavaBean之間的轉(zhuǎn)換建議使用TypeReference類 (1)json字符串-簡單對象與Java對象之間的轉(zhuǎn)換 // 方法1 Student student = JSON.parseObject(jsonStr , new TypeReference() {}); // 方法2 Student student = JSON.parseObject(jsonStr , Student.class); 1 2 3 4 (2)json字符串-數(shù)組與Java對象之間的轉(zhuǎn)換 ArrayListstudents = JSON.parseObject(jsonArrStr, new TypeReference() {}); for (Student student : students) { System.out.println(student.getStudentName()+":"+student.getStudentAge()); } 1 2 3 4 (3)json字符串-復雜對象與Java對象之間的轉(zhuǎn)換 Teacher teacher = JSON.parseObject(complexJsonStr, new TypeReference() {}); //獲取teacher中的內(nèi)容 String teacherName = teacher.getTeacherName(); Integer teacherAge = teacher.getTeacherAge(); Course course = teacher.getCourse(); Liststudents = teacher.getStudents(); 1 2 3 4 5 6 4.Java對象到JSON字符串的轉(zhuǎn)換 Teacher teacher = new Teacher(); String jsonStr = JSON.toJSONString(teacher); 1 2 5.Java對象到JSON對象的轉(zhuǎn)換 String jsonStr= JSON.toJSONString(student); JSONObject jsonObj = JSON.parseObject(jsonStr); 1 2 6.JSON對象到Java對象的轉(zhuǎn)換 # 方法1,先轉(zhuǎn)換為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字符串轉(zhuǎn)化為java實體類 (parseObject) ApprovalVo approvalVo = JSON.parseObject(str, ApprovalVo.class); // str == json字符串 // ApprovalVo == 實體類 1 2 3 2.json字符串轉(zhuǎn)化為list對象 (parseArray) String str2 = "[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]"; Listusers = JSON.parseArray(jsonStr2, User.class); 1 2 3.json字符串轉(zhuǎn)化為復雜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 把實體類轉(zhuǎn)化成json字符串 String str = JSON.toJSONString(ApprovalVo); 1 把json字符串轉(zhuǎn)化成JSONObject String jsonStr = "{\"school\":\"商職\",\"sex\":\"男\(zhòng)",\"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 ———————————————— 版權(quán)聲明:本文為CSDN博主「皮皮蝦的倔強」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/Wolves_howl/article/details/124921713 |
|
|