1.创建实体类Teacher.java
package com.imooc.sell.json;
import java.util.List;
public class Teacher {
private String teacherName;
private Integer teacherAge;
private Course course;
private List<Student> students;
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Integer getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(Integer 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;
}
public Teacher(String teacherName, Integer teacherAge, Course course, List<Student> students) {
this.teacherName = teacherName;
this.teacherAge = teacherAge;
this.course = course;
this.students = students;
}
public Teacher() {
}
@Override
public String toString() {
return "Teacher{" +
"teacherName='" + teacherName + '\'' +
", teacherAge=" + teacherAge +
", course=" + course +
", students=" + students +
'}';
}
}
2.创建实体类Course.java
package com.imooc.sell.json;
public class Course {
private String courseName;
private Integer code;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Course() {
}
public Course(String courseName, Integer code) {
this.courseName = courseName;
this.code = code;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", code=" + code +
'}';
}
}
3.创建实体类Student.java
package com.imooc.sell.json;
public class Student {
private String studentName;
private Integer studentAge;
public Student() {
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
public Student(String studentName, Integer studentAge) {
this.studentName = studentName;
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", studentAge=" + studentAge +
'}';
}
}
4.创建测试代码
package com.imooc.sell.json;
/*
*
* 学习fastjson
* Date:2018-08-01
* */
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FastJsonTest {
//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}]}";
/*
* 1.JSON格式字符串与JSON对象之间的转换
*/
@Test
public void testComplexJSONStrToJSONObject(){
/*
* 复杂json格式字符串与JSONObject之间的转换
* */
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
//获取单值
System.out.println(jsonObject.get("teacherName"));
//获取json对象
JSONObject course = jsonObject.getJSONObject("course");
System.out.println(course.get("courseName"));
//获取数组
JSONArray students = jsonObject.getJSONArray("students");
System.out.println(students.getJSONObject(1).get("studentName"));
}
/**
* 复杂JSONObject到json格式字符串的转换
*/
@Test
public void testJSONObjectToComplexJSONStr() {
//复杂JSONObject,目标要转换为json字符串
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
String string = jsonObject.toString();
System.out.println(string);
}
/*
*
*2.JSON格式字符串与javaBean之间的转换
*
*/
@Test
public void testComplexJSONStrToJavaBean(){
/**
* 复杂json格式字符串到JavaBean_obj的转换
*/
//使用Gson思想
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
System.out.println(teacher.getTeacherName());
System.out.println(teacher.getCourse().getCourseName());
System.out.println(teacher.getStudents().get(1).getStudentName());
}
/**
* 复杂JavaBean_obj到json格式字符串的转换
*/
@Test
public void testJavaBeanToComplexJSONStr(){
//先转成Bean
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
//转成json字符串
String jsonString = JSONObject.toJSONString(teacher);
System.out.println(jsonString);
}
/*
*
* 3.javaBean与json对象间的之间的转换
*
* */
/**
* 复杂JavaBean_obj到json对象的转换
*/
@Test
public void testComplexJavaBeanToJSONObject(){
//先转成Bean
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
//转成json对象
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(teacher);
System.out.println(jsonObject.getJSONObject("course").get("courseName"));
}
/**
* 复杂json对象到JavaBean_obj的转换
*/
@Test
public void testComplexJSONObjectToJavaBean(){
//先转成Bean
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
//转成json对象
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(teacher);
//转成Bean
Teacher teacher1 = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), Teacher.class);
System.out.println(teacher1.getTeacherName());
System.out.println(teacher1.getCourse().getCourseName());
System.out.println(teacher1.getStudents().get(1).getStudentName());
}
}