json的解析最主要的工作就是,javaBean對象,json對象,json格式字符串之間的相互轉換
參考博客:寫的比較全面,簡單易懂
https://segmentfault.com/a/1190000011212806
-
JSON格式字符串 ←→JSONObject對象
- JSON格式字符串 →JSONObject對象
JSONObject jsonObject =JSONObject.parseObject((JSON_OBJ_STR); JSONArray jsonArray =JSONArray.parseArray(JSON_ARRAY_STR); for (Object object : jsonArraystudents) { JSONObject jsonObjectone = (JSONObject) object; }
- JSONObject對象 →JSON格式字符串
// 第一種方式 String jsonString = JSONObject.toJSONString(jsonObject); // 第二種方式 String jsonString = jsonObject.toJSONString();
2. JSON格式字符串 ←→javaBean對象
- JSON格式字符串 →javaBean對
//第一種方式 JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); String studentName = jsonObject.getString("studentName"); Integer studentAge = jsonObject.getInteger("studentAge"); Student student = new Student(studentName, studentAge); List<Student> students = new ArrayList<Student>(); Student student = null; for (Object object : jsonArray) { JSONObject jsonObjectone = (JSONObject) object; String studentName = jsonObjectone.getString("studentName"); Integer studentAge = jsonObjectone.getInteger("studentAge"); student = new Student(studentName,studentAge); students.add(student); } //第二種方式,使用TypeReference<T>類,由於其構造方法使用protected進行修飾,故創建其子類 Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>(); List<Student> studentList = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {}); //第三種方式,使用Gson的思想 Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class); List<Student> studentList1 = JSONArray.parseArray(JSON_ARRAY_STR, Student.class);
- javaBean對象 →JSON格式字符串
String jsonString = JSONObject.toJSONString(student); List<Student> students = new ArrayList<Student>(); String jsonString = JSONArray.toJSONString(students);
3.JSONObject對象 ←→javaBean對象(以JSON字符串爲媒介)
- javaBean對象→JSONObject對象
//方式一 String jsonString = JSONObject.toJSONString(student); JSONObject jsonObject = JSONObject.parseObject(jsonString); String jsonString = JSONArray.toJSONString(students); JSONArray jsonArray = JSONArray.parseArray(jsonString); //方式二 JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student); JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students);
- JSONObject→javaBean對象
//第一種方式,使用TypeReference<T>類,由於其構造方法使用protected進行修飾,故創建其子類 Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {}); ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(), new TypeReference<ArrayList<Student>>() {}); //第二種方式,使用Gson的思想 Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class); List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);
另外還有以下兩個成員方法用的比較多:
JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");
JSONArray jsonArraystudents = jsonObject.getJSONArray("students");
public void testComplexJSONStrToJSONObject() {
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
System.out.println("teacherName: " + teacherName + " teacherAge: " + teacherAge);
JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");
//獲取JSONObject中的數據
String courseName = jsonObjectcourse.getString("courseName");
Integer code = jsonObjectcourse.getInteger("code");
System.out.println("courseName: " + courseName + " code: " + code);
JSONArray jsonArraystudents = jsonObject.getJSONArray("students");
//遍歷JSONArray
for (Object object : jsonArraystudents) {
JSONObject jsonObjectone = (JSONObject) object;
String studentName = jsonObjectone.getString("studentName");
Integer studentAge = jsonObjectone.getInteger("studentAge");
System.out.println("studentName: " + studentName + " studentAge: " + studentAge);
}
}