json的解析最主要的工作就是,javaBean对象,json对象,json格式字符串之间的相互转换
最常用:
// json字符串-> javabean
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);
参考博客:写的比较全面,简单易懂
高性能JSON框架之FastJson的简单使用 - SegmentFault 思否
注意:
json格式字符串需要满足:
String mediaList = "[{\"mediaId\":11,\"mediaName\":\"abc\"},{\"mediaId\":12,\"mediaName\":\"abc2\"}]";
1.mediaId、mediaName可以不用""引号;但是其对应的值(如上的abc,abc2)如果是字符串,必须需要""用引号;
-
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);
}
}