导入com.alibaba.fastjson
第一步:先创建3个javaBean类
Teacher类:
public class Teacher {
private String teacherName;
private Integer teacherAge;
private Course course;
private List students;
…
此处省去get/set方法
…
Student类:
public class Student {
private String studentName;
private Integer studentAge;
…
此处省去get/set方法
…
}
Course类
public class Course {
private String courseName;
private Integer code;
…
此处省去get/set方法
…
}
第二步: 创建JSONUtils类
public class JSONUtils {
//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_str_str = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
/**
* json字符串-简单类型转换为JSONObject
*/
@Test
public void toJSONStrToJSONObject() {
JSONObject jsonObject = JSON.parseObject(json_obj_str);
System.out.println(jsonObject.getString("studentName") + jsonObject.getInteger("studentAge"));
}
/**
* json字符串-数组类型转换为JSONObjcet
*/
@Test
public void toJSONStrToJSONArray() {
JSONArray jsonArray = JSON.parseArray(json_array_str);
//遍历
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName") + jsonObject.getString("studentAge"));
}
}
/**
* JSON字符串-复杂类型转换为JSONObject
*/
@Test
public void toJSONStrToJSONComplex() {
JSONObject jsonObject = JSON.parseObject(complex_str_str);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
//获取对象里的值
JSONObject course = jsonObject.getJSONObject("course");
String courseName = course.getString("courseName");
Integer code = course.getInteger("code");
//获取数组里的值
JSONArray students = jsonObject.getJSONArray("students");
String studentName = null;
int studentAge = 0;
for (int i = 0; i < students.size(); i++) {
JSONObject jsonObject1 = students.getJSONObject(i);
studentName = jsonObject1.getString("studentName");
studentAge = jsonObject1.getInteger("studentAge");
}
System.out.println("teacherName:" + teacherName);
System.out.println("teacherAge:" + teacherAge);
System.out.println("courseName:" + courseName);
System.out.println("code:" + code);
System.out.println("studentName:" + studentName);
System.out.println("studentAge:" + studentAge);
}
/**
* json字符串-简单类型转换为javabean对象
*/
@Test
public void toJSONStrToJavaBeanObj() {
Student student = JSON.parseObject(json_obj_str, new TypeReference<Student>() {
});
System.out.println(student.getStudentName() + student.getStudentAge());
}
/**
* json字符串-数组类型转换为javabean对象
*/
@Test
public void toJSONStrToJavaBeanList() {
ArrayList<Student> studentsList = JSON.parseObject(json_array_str, new TypeReference<ArrayList<Student>>() {
});
Iterator<Student> iterator = studentsList.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
System.out.println(student.getStudentName() + student.getStudentAge());
}
}
/**
* java字符串-复杂类型转换为javaBean对象
*/
@Test
public void toJSONStrComplexToJavaBean() {
Teacher teacher = JSON.parseObject(complex_str_str, new TypeReference<Teacher>() {
});
String teacherName = teacher.getTeacherName();
Integer teacherAge = teacher.getTeacherAge();
Course course = teacher.getCourse();
String courseName = course.getCourseName();
Integer code = course.getCode();
List<Student> studentsList = teacher.getStudents();
for (int i = 0; i <studentsList.size() ; i++) {
Student student = studentsList.get(i);
System.out.println(student.getStudentName()+"----"+student.getStudentAge());
}
System.out.println("teacherName:"+teacherName);
System.out.println("teacherAge:"+teacherAge);
System.out.println("courseName:"+courseName);
System.out.println("code:"+code);
}
}
1330

被折叠的 条评论
为什么被折叠?



