JSON入门学习
package com.my.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.my.entity.Student; import com.my.entity.Teacher; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; /** * @author : lijie * @version V1.0 * @Description: * @date Date : 2021年07月15日 21:05 */ public class JSONDemo { /** * 单个实例:bean<==>json */ @Test public void test1() { System.out.println("------The Result of test1------"); // bean==>json Student s1 = new Student(1, "zhangsan", 18); String s = JSON.toJSONString(s1); System.out.println(s); // json ==> bean String json = "{\"age\":18,\"id\":1,\"name\":\"zhangsan\"}"; // 1.直接利用JSONObject来获取相应属性值 System.out.println("--------------1--------------"); JSONObject jsonObject = JSON.parseObject(json); String name = jsonObject.getString("name"); System.out.println(name); int age = jsonObject.getIntValue("age"); System.out.println(age); // 2.将串转成相应java对象,然后再获取属性 System.out.println("--------------2--------------"); Student student = JSON.parseObject(json, Student.class); System.out.println(student); } /** * 多个实例:bean<==>json */ @Test public void test2() { System.out.println("------The Result of test2------"); // bean==>json List<Student> students = Arrays .asList(new Student(1, "zhangsan", 18), new Student(2, "lisi", 19)); String studentsJSON = JSON.toJSONString(students); System.out.println(studentsJSON); // json ==> bean String json = "[{\"age\":18,\"id\":1,\"name\":\"zhangsan\"},{\"age\":19,\"id\":2,\"name\":\"lisi\"}]"; // 利用JSONArray获取相应属性值 JSONArray jsonArray = JSON.parseArray(json); System.out.println(jsonArray.get(0)); System.out.println(jsonArray.get(1)); // 直接获取相应的bean对象集合 List<Student> studentList = JSON.parseArray(json, Student.class); System.out.println(studentList); } @Test public void test3() { System.out.println("------The Result of test3------"); List<Student> students = Arrays .asList(new Student(1, "zhangsan", 18), new Student(2, "lisi", 19)); List<Student> students1 = Arrays .asList(new Student(3, "wangwu", 20), new Student(4, "qianliu", 21)); List<List<Student>> lists = new ArrayList<>(); lists.add(students); lists.add(students1); String jsonString = JSON.toJSONString(lists); System.out.println(jsonString); System.out.println("------------------------------------"); String json = "[[{\"age\":18,\"id\":1,\"name\":\"zhangsan\"},{\"age\":19,\"id\":2,\"name\":\"lisi\"}],[{\"age\":20,\"id\":3,\"name\":\"wangwu\"},{\"age\":21,\"id\":4,\"name\":\"qianliu\"}]]"; JSONArray jsonArray = JSON.parseArray(json); System.out.println(jsonArray.get(0)); System.out.println(jsonArray.get(1)); String s = JSON.toJSONString(jsonArray.get(1)); JSONArray jsonArray1 = JSON.parseArray(s); System.out.println(jsonArray1.get(0)); System.out.println(jsonArray1.get(1)); List<Student> students2 = JSON.parseArray(s, Student.class); System.out.println(students2); } }
===========================================结果如下==================================================
------The Result of test1------
{"age":18,"id":1,"name":"zhangsan"}
--------------1--------------
zhangsan
18
--------------2--------------
Student(id=1, name=zhangsan, age=18)
------The Result of test2------
[{"age":18,"id":1,"name":"zhangsan"},{"age":19,"id":2,"name":"lisi"}]
{"name":"zhangsan","id":1,"age":18}
{"name":"lisi","id":2,"age":19}
[Student(id=1, name=zhangsan, age=18), Student(id=2, name=lisi, age=19)]
------The Result of test3------
[[{"age":18,"id":1,"name":"zhangsan"},{"age":19,"id":2,"name":"lisi"}],[{"age":20,"id":3,"name":"wangwu"},{"age":21,"id":4,"name":"qianliu"}]]
------------------------------------
[{"name":"zhangsan","id":1,"age":18},{"name":"lisi","id":2,"age":19}]
[{"name":"wangwu","id":3,"age":20},{"name":"qianliu","id":4,"age":21}]
{"name":"wangwu","id":3,"age":20}
{"name":"qianliu","id":4,"age":21}
[Student(id=3, name=wangwu, age=20), Student(id=4, name=qianliu, age=21)]