原文链接:https://zhuanlan.zhihu.com/p/62763428
json字符串->JSONObject
用JSON.parseObject()方法即可将JSon字符串转化为JSON对象,利用JSONObject中的get()方法来获取JSONObject中的相对应的键对应的值
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.jiyong.config.Student; import com.jiyong.config.Teacher; import org.junit.Test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Fastjson用法 */ public class FastJsonOper { //json字符串-简单对象型,加\转义 private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}"; //json字符串-数组类型 private static final String JSON_ARRAY_STR = " [{\"studentName\":\"lily\",\"studentAge\":12}," + "{\"studentName\":\"lucy\",\"studentAge\":15}]"; //复杂格式json字符串 private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\"," + "\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}"; // json字符串与JSONObject之间的转换 @Test public void JsonStrToJSONObject(){ JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); System.out.println("StudentName: " + jsonObject.getString("studentName") + "," + "StudentAge: " + jsonObject.getInteger("studentAge")); } }
JSONObject->json字符串
用JSON.toJSONString()方法即可将JSON对象转化为JSON字符串
/** * 将JSONObject转换为JSON字符串,用JSON.toJSONString()方法即可将JSON字符串转化为JSON对象 */ @Test public void JSONObjectToJSONString(){ JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); String s = JSON.toJSONString(jsonObject); System.out.println(s); }
JSON字符串数组->JSONArray
将JSON字符串数组转化为JSONArray,通过JSON的parseArray()方法。JSONArray本质上还是一个数组,对其进行遍历取得其中的JSONObject,然后再利用JSONObject的get()方法取得其中的值。有两种方式进行遍历
- 方式一:通过jsonArray.size()获取JSONArray中元素的个数,再通过getJSONObject(index)获取相应位置的JSONObject,循环变量取得JSONArray中的JSONObject,再利用JSONObject的get()进行取值。
- 方式二:通过jsonArray.iterator()获取迭代器
/** * 将JSON字符串数组转化为JSONArray,通过JSON的parseArray()方法 */ @Test public void JSONArrayToJSONStr(){ JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); /** * JSONArray本质上还是一个数组,对其进行遍历取得其中的JSONObject,然后再利用JSONObject的get()方法取得其中的值 * 方式一是通过jsonArray.size()获取JSONArray中元素的个数, * 再通过getJSONObject(index)获取相应位置的JSONObject,在利用JSONObject的get()进行取值 * 方式二是通过jsonArray.iterator()获取迭代器 * */ // 遍历方式一 // int size = jsonArray.size(); // for(int i = 0;i < size;i++){ // JSONObject jsonObject = jsonArray.getJSONObject(i); // System.out.println("studentName: " + jsonObject.getString("studentName") + ",StudentAge: " + jsonObject.getInteger("studentAge")); // } // 遍历方式二 Iterator<Object> iterator = jsonArray.iterator(); while (iterator.hasNext()){ JSONObject jsonObject = (JSONObject) iterator.next(); System.out.println("studentName: " + jsonObject.getString("studentName") + ",StudentAge: " + jsonObject.getInteger("studentAge")); } }
JSONArray->json字符串
用JSON.toJSONString()方法即可将JSONArray转化为JSON字符串
/** * JSONArray到json字符串-数组类型的转换 */ @Test public void JSONArrayToJSONString(){ JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); String s = JSON.toJSONString(jsonArray); System.out.println(s); }
复杂JSON格式字符串->JSONObject
将复杂JSON格式字符串转换为JSONObject,也是通过JSON.parseObject()
/** * 将复杂JSON格式字符串转换为JSONObject,也是通过JSON.parseObject(),可以取其中的部分 */ @Test public void JSONStringTOJSONObject(){ JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); // 获取简单对象 String teacherName = jsonObject.getString("teacherName"); Integer teacherAge = jsonObject.getInteger("teacherAge"); System.out.println("teacherName: " + teacherName + ",teacherAge " + teacherAge); // 获取JSONObject对象 JSONObject course = jsonObject.getJSONObject("course"); // 获取JSONObject中的数据 String courseName = course.getString("courseName"); Integer code = course.getInteger("code"); System.out.println("courseName: " + courseName + " code: " + code); // 获取JSONArray对象 JSONArray students = jsonObject.getJSONArray("students"); // 获取JSONArray的中的数据 Iterator<Object> iterator = students.iterator(); while (iterator.hasNext()){ JSONObject jsonObject1 = (JSONObject) iterator.next(); System.out.println("studentName: " + jsonObject1.getString("studentName") + ",StudentAge: " + jsonObject1.getInteger("studentAge")); } }
复杂JSONObject->json字符串
用JSON.toJSONString()方法即可将复杂JSONObject转化为JSON字符串
/** * 复杂JSONObject到json字符串的转换 */ @Test public void JSONObjectTOJSON(){ JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); String s = JSON.toJSONString(jsonObject); System.out.println(s); }
json字符串->JavaBean
定义JavaBean类
package com.fastjson; public class Student { private String studentName; private int studentAge; public Student() { } public Student(String studentName, int studentAge) { this.studentName = studentName; this.studentAge = studentAge; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } @Override public String toString() { return "Student{" + "studentName=\'" + studentName + \'\\'\' + ", studentAge=" + studentAge + \'}\'; } } package com.jiyong.config; /** * 对于复杂嵌套的JSON格式,利用JavaBean进行转换的时候要注意 * 1、有几个JSONObject就定义几个JavaBean * 2、内层的JSONObject对应的JavaBean作为外层JSONObject对应的JavaBean的一个属性 * 3、解析方法有两种 * 第一种方式,使用TypeReference<T>类 * Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {}) * 第二种方式,使用Gson思想 * Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class); */ import java.util.List; public class Teacher { private String teacherName; private int teacherAge; private Course course; private List<Student> students; public Teacher() { } public Teacher(String teacherName, int teacherAge, Course course, List<Student> students) { this.teacherName = teacherName; this.teacherAge = teacherAge; this.course = course; this.students = students; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public int getTeacherAge() { return teacherAge; } public void setTeacherAge(int teacherAge) { this.teacherAge = teacherAge; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String toString() { return "Teacher{" + "teacherName=\'" + teacherName + \'\\'\' + ", teacherAge=" + teacherAge + ", course=" + course + ", students=" + students + \'}\'; } } package com.jiyong.config; public class Course { private String courseName; private int code; public Course() { } public Course(String courseName, int code) { this.courseName = courseName; this.code = code; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } @Override public String toString() { return "Course{" + "courseName=\'" + courseName + \'\\'\' + ", code=" + code + \'}\'; } }
Jason字符串转换为JavaBean有三种方式,推荐通过反射的方式。
/** * json字符串-简单对象到JavaBean之间的转换 * 1、定义JavaBean对象 * 2、Jason字符串转换为JavaBean有三种方式,推荐通过反射的方式 */ @Test public void JSONStringToJavaBeanObj(){ // 第一种方式 JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); String studentName = jsonObject.getString("studentName"); Integer studentAge = jsonObject.getInteger("studentAge"); Student student = new Student(studentName, studentAge); // 第二种方式,//第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类 Student student1 = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); // 第三种方式,通过反射,建议这种方式 Student student2 = JSON.parseObject(JSON_OBJ_STR, Student.class); }
JavaBean ->json字符串
也是通过JSON的toJSONString,不管是JSONObject、JSONArray还是JavaBean转为为JSON字符串都是通过JSON的toJSONString方法。
/** * JavaBean转换为Json字符串,也是通过JSON的toJSONString,不管是JSONObject、JSONArray还是JavaBean转为为JSON字符串都是通过JSON的toJSONString方法 */ @Test public void JavaBeanToJsonString(){ Student lily = new Student("lily", 12); String s = JSON.toJSONString(lily); System.out.println(s); }
json字符串数组->JavaBean-List
/** * json字符串-数组类型到JavaBean_List的转换 */ @Test public void JSONStrToJavaBeanList(){ // 方式一: JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); //遍历JSONArray List<Student> students = new ArrayList<Student>(); Iterator<Object> iterator = jsonArray.iterator(); while (iterator.hasNext()){ JSONObject next = (JSONObject) iterator.next(); String studentName = next.getString("studentName"); Integer studentAge = next.getInteger("studentAge"); Student student = new Student(studentName, studentAge); students.add(student); } // 方式二,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类 List<Student> studentList = JSON.parseObject(JSON_ARRAY_STR,new TypeReference<ArrayList<Student>>() {}); // 方式三,使用反射 List<Student> students1 = JSON.parseArray(JSON_ARRAY_STR, Student.class); System.out.println(students1); }
JavaBean-List ->json字符串数组
/** * JavaBean_List到json字符串-数组类型的转换,直接调用JSON.toJSONString()方法即可 */ @Test public void JavaBeanListToJSONStr(){ Student student = new Student("lily", 12); Student student1 = new Student("lucy", 13); List<Student> students = new ArrayList<Student>(); students.add(student); students.add(student1); String s = JSON.toJSONString(student); System.out.println(s); }
复杂嵌套json格式字符串->JavaBean_obj
对于复杂嵌套的JSON格式,利用JavaBean进行转换的时候要注意:
- 有几个JSONObject就定义几个JavaBean
- 内层的JSONObject对应的JavaBean作为外层JSONObject对应的JavaBean的一个属性
/** * 复杂json格式字符串到JavaBean_obj的转换 */ @Test public void ComplexJsonStrToJavaBean(){ //第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类 Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {}); // 第二种方式,使用反射 Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, Teacher.class); }
JavaBean_obj->复杂json格式字符串
/** * 复杂JavaBean_obj到json格式字符串的转换 */ @Test public void JavaBeanToComplexJSONStr(){ Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, Teacher.class); String s = JSON.toJSONString(teacher); System.out.println(s); }