Student实体类
@Data
@AllArgsConstructor
public class Student {
private String name;
private int age;
private List<Course> courses;
}
course实体类
@Data
@AllArgsConstructor
public class Course {
private String name;
private int score;
}
一、JSON对象转成JSON字符串
1.JSONObject转成字符串
jsonObject.toJSONString()
@Test
public void testJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", 23);
List<Course> courses = new ArrayList<>();
courses.add(new Course("语文", 100));
courses.add(new Course("数学",80));
jsonObject.put("courses", courses);
String str = jsonObject.toString();
System.out.println(str);
//{"courses":[{"name":"语文","score":100},{"name":"数学","score":80}],"name":"张三","age":23}
}
jsonObject.toString()
@Test
public void testJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", 23);
List<Course> courses = new ArrayList<>();
courses.add(new Course("语文", 100));
courses.add(new Course("数学",80));
jsonObject.put("courses", courses);
String str = jsonObject.toString();
System.out.println(str);
//{"courses":[{"name":"语文","score":100},{"name":"数学","score":80}],"name":"张三","age":23}
}
2.JSONArray转成字符串
jsonArray.toJSONString() 和 jsonArray.toString() 都可以
@Test
public void testJson() {
JSONArray jsonArray = new JSONArray();
jsonArray.add(new Course("语文", 100));
jsonArray.add(new Course("数学",80));
String str = jsonArray.toJSONString();
System.out.println(str);
//[{"name":"语文","score":100},{"name":"数学","score":80}]
}
二、JSON对象转成JAVA对象
1.JSONObject转成对象
jsonObject.toJavaObject()
@Test
public void testJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", 23);
List<Course> courses = new ArrayList<>();
courses.add(new Course("语文", 100));
courses.add(new Course("数学",80));
jsonObject.put("courses", courses);
//JSON转成对象
Student student = jsonObject.toJavaObject(Student.class);
System.out.println(student);
//Student(name=张三, age=23, courses=[Course(name=语文, score=100), Course(name=数学, score=80)])
}
2.JSONArray转成对象
jsonArray.toJavaList()
@Test
public void testJson() {
JSONArray jsonArray = new JSONArray();
jsonArray.add(new Course("语文", 100));
jsonArray.add(new Course("数学", 80));
//JSON转成List对象
List<Course> courses = jsonArray.toJavaList(Course.class);
System.out.println(courses);
//[Course(name=语文, score=100), Course(name=数学, score=80)]
}
三、JSON字符串转JSON对象
1.字符串转JSON对象
JSONObject.parseObject()
@Test
public void testJson() {
String str = "{\"age\":18,\"courses\":[{\"name\":\"math\",\"score\":98},{\"name\":\"english\",\"score\":88}],\"name\":\"zhangsan\"}";
JSONObject jsonObject = JSONObject.parseObject(str);
System.out.println(jsonObject);
//{"courses":[{"score":98,"name":"math"},{"score":88,"name":"english"}],"name":"zhangsan","age":18}
}
2.集合字符串转成JSONArray对象
JSONOArray.parseArray()
@Test
public void testJson() {
String str = "[{\"name\":\"语文\",\"score\":100},{\"name\":\"数学\",\"score\":80}]";
JSONArray jsonArray = JSONArray.parseArray(str);
System.out.println(jsonArray);
//[{"score":100,"name":"语文"},{"score":80,"name":"数学"}]
}
四、JAVA对象转成JSON字符串
1.Java对象转成JSON字符串
JSONObject.toJSONString()
@Test
public void testJson() {
Student student = new Student("zhangsan", 18, new ArrayList<Course>() {{
add(new Course("math", 98));
add(new Course("english", 88));
}});
String json = JSONObject.toJSONString(student);
System.out.println(json);
//{"age":18,"courses":[{"name":"math","score":98},{"name":"english","score":88}],"name":"zhangsan"}
}
2.Java集合转成JSON字符串
JSONObject.toJSONString() 和 JSONArray.toJSONString() 都可以
@Test
public void testJson() {
List<Course> courses = new ArrayList<>();
courses.add(new Course("语文", 90));
courses.add(new Course("数学", 80));
// 集合转json
String json = JSONArray.toJSONString(courses);
System.out.println(json);
//[{"name":"语文","score":90},{"name":"数学","score":80}]
}
五、JSON字符串转成JAVA对象
1.JSON字符串转JAVA对象
JSONObject.parseObject(jsonStr, Student.class)
@Test
public void testJson() {
String jsonStr = "{\"age\":18,\"courses\":[{\"name\":\"语文\",\"score\":90},{\"name\":\"数学\",\"score\":80},{\"name\":\"英语\",\"score\":70}],\"name\":\"张三\"}";
// 将 JSON 字符串转换为学生对象
Student student = JSONObject.parseObject(jsonStr, Student.class);
System.out.println(student);
//Student(name=张三, age=18, courses=[Course(name=语文, score=90), Course(name=数学, score=80), Course(name=英语, score=70)])
}
2.JSON字符串转List集合
JSONArray.parseArray(str, Course.class);
@Test
public void testJson() {
String str = "[{\"name\":\"语文\",\"score\":100},{\"name\":\"数学\",\"score\":80}]";
List<Course> courses = JSONArray.parseArray(str, Course.class);
System.out.println(courses);
//[Course(name=语文, score=100), Course(name=数学, score=80)]
}