1 JSON,JSONArray,JSONObject,必须掌握三者的关系,因为后面的方法中我们经常使用JSONObject中的方法,其实调用的是JSON
2 JSON类之parseObject()方法,实现json字符串转换为json对象或javabean对象
23JSON类之toJSONString()方法,实现json对象转化为json字符串和javabean对象转化为json 字符串,
总结:
1 这两个方法我们经常使用,因为我们往往会从前端得到一段字符串,然后使用parseObject转换为我们后端的javabean,
使用toJSONString,把我们的json对象转换为json字符串。
2 这里面我们一定要清楚,我们传输的json字符串还是json对象,使用RestController,采用的是json字符串。
最后贴出自己的例子,供参考:
package com.ying.test;
import java.util.ArrayList;
import org.junit.Test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ying.pojo.Course;
import com.ying.pojo.Student;
public class TestJson {
//json字符串-简单对象型
private static final String JSON_OBJ_STR = "{\"STUDENTNAM\":\"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}]}";
private static final SerializerFeature[] features =
{
SerializerFeature.WriteMapNullValue, // 输出空置字段
//SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
//SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
//SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
};
@Test
public void testJson2JavaBean() {
Student student = JSONObject.parseObject(JSON_OBJ_STR,Student.class);
System.out.println("name:" + student.getStudentName() + " age:" + student.getStudentAge());
}
//测试是否能正常映射
@Test
public void testToString() {
Student stu = new Student();
stu.setStudentAge(11);
stu.setStudentName("lily");
String stuString = JSONObject.toJSONString(stu,features);
System.out.println(stuString);
}
//测试filed是否有效果
@Test
public void testToString2() {
Student stu = new Student();
stu.setStudentAge(11);
String stuString = JSONObject.toJSONString(stu,features);
System.out.println(stuString);
}
@Test
public void testJsonArray2JavaBean() {
JSONArray jsonArray = JSONObject.parseArray(JSON_ARRAY_STR);
for(Object object : jsonArray) {
JSONObject jsonObject = (JSONObject)object;
Student student = JSONObject.toJavaObject(jsonObject, Student.class);
System.out.println("name:" + student.getStudentName() + " age:" + student.getStudentAge());
}
}
@Test
public void testJsonArrayToString2() {
ArrayList<Student > arrayList = new ArrayList<>();
Student student1 = new Student();
student1.setStudentAge(11);
Student student2 = new Student();
student2.setStudentAge(12);
arrayList.add(student1);
arrayList.add(student2);
JSONArray jArray = (JSONArray)JSONArray.toJSON(arrayList);
String stuString = JSONObject.toJSONString(jArray,features);
System.out.println(stuString);
}
@Test
public void testFixJson2JavaBean() {
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
String teacherName = jsonObject.getString("teacherName");
String teacherAge = jsonObject.getString("teacherAge");
System.out.println("teacherName"+teacherName + "teacherAge" + teacherAge);
JSONObject jsonObject1 = jsonObject.getJSONObject("course");
Course course = JSONObject.toJavaObject(jsonObject1,Course.class);
System.out.println("Course:" +course.toString());
JSONArray jsonArray = jsonObject.getJSONArray("students");
for(Object object : jsonArray) {
JSONObject jsonObject2 = (JSONObject)object;
Student student = JSONObject.toJavaObject(jsonObject2, Student.class);
System.out.println("name:" + student.getStudentName() + " age:" + student.getStudentAge());
}
}
}
参考https://blog.csdn.net/srj1095530512/article/details/82529759