//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 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对象之间的转换。
示例1.1-json字符串-简单对象型与JSONObject之间的转换
/**
* json字符串-简单对象型与JSONObject之间的转换
*/
public static void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
示例1.2-json字符串-数组类型与JSONArray之间的转换
/**
* json字符串-数组类型与JSONArray之间的转换
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的
//遍历方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
//遍历方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
示例2:JSON格式字符串与javaBean之间的转换。
首先,我们针对数据源所示的字符串,提供三个javaBean。
public class Student {
private String studentName;
private Integer studentAge;
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 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 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;
}
}
json字符串与javaBean之间的转换推荐使用 TypeReference<T> 这个类,使用泛型可以更加清晰,当然也有其它的转换方式,这里就不做探讨了。
示例2.1-json字符串-简单对象型与javaBean之间的转换
/**
* json字符串-简单对象与JavaBean_obj之间的转换
*/
public static void testJSONStrToJavaBeanObj(){
Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
//Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
示例2.2-json字符串-数组类型与javaBean之间的转换
/**
* json字符串-数组类型与JavaBean_List之间的转换
*/
public static void testJSONStrToJavaBeanList(){
ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
//ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//因为JSONArray继承了JSON,所以这样也是可以的
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}
示例2.3-复杂json格式字符串与与javaBean之间的转换
/**
* 复杂json格式字符串与JavaBean_obj之间的转换
*/
public static void testComplexJSONStrToJavaBean(){
Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});
//Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});//因为JSONObject继承了JSON,所以这样也是可以的
String teacherName = teacher.getTeacherName();
Integer teacherAge = teacher.getTeacherAge();
Course course = teacher.getCourse();
List<Student> students = teacher.getStudents();
}
转载至https://www.cnblogs.com/cdf-opensource-007/p/7106018.html