package test; import com.alibaba.fastjson.JSON; import org.junit.Test; import test.Student; import java.util.*; import static com.alibaba.fastjson.JSON.parseArray; public class testFastJson { public Date getDate(){ Date date = new Date(); return date; } @Test //json格式字符串,反序列化为Map集合 public void testJsonToMap(){ String jsonStr = "{\"student2\":{\"address\":\"北京\",\"age\":18,\"birthday\":1714560509775,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"},\"student1\":{\"address\":\"北京\",\"age\":18,\"birthday\":1714560509775,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}}\n"; Map<String, Object> map = JSON.parseObject(jsonStr); System.out.println(map); } @Test // json格式字符串,反序列化为List集合 public void testJsonToList(){ String jsonStr = "[{\"address\":\"北京\",\"age\":18,\"birthday\":1714543419605,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}, {\"address\":\"北京\",\"age\":18,\"birthday\":1714543419605,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}]\n"; List<Student> students = JSON.parseArray(jsonStr, Student.class); for (Student student : students) { System.out.println(student); } } @Test //json格式字符串,反序列化为java对象 public void testJsonToObject(){ String jsonString = "{\"address\":\"北京\",\"age\":18,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}"; Student student1 = JSON.parseObject(jsonString, Student.class); System.out.println(student1); } // ==================================================================================== @Test //map集合,序列化json格式的字符串 public void testMapToJson(){ Map<String, Student> map = new HashMap<>(); Student student1 = new Student(); student1.setName("小米"); student1.setAge(18); student1.setSex("男"); student1.setMail("123.qq.com"); student1.setAddress("北京"); student1.setBirthday(getDate()); Student student2 = new Student(); student2.setName("小米"); student2.setAge(18); student2.setSex("男"); student2.setMail("123.qq.com"); student2.setAddress("北京"); student2.setBirthday(getDate()); map.put("student1", student1); map.put("student2", student2); // json格式的字符串是对象,,对象中有2个键,,,键对应的值是Sttudent对象 //{"student2":{"address":"北京","age":18,"birthday":1714560509775,"mail":"123.qq.com","name":"小米","sex":"男"}, // "student1":{"address":"北京","age":18,"birthday":1714560509775,"mail":"123.qq.com","name":"小米","sex":"男"}} String str = JSON.toJSONString(map); System.out.println(str); } @Test //list集合,序列化json格式的字符串 public void testListToJson(){ List<Student> list = new ArrayList<>(); Student student1 = new Student(); student1.setName("小米"); student1.setAge(18); student1.setSex("男"); student1.setMail("123.qq.com"); student1.setAddress("北京"); student1.setBirthday(getDate()); Student student2 = new Student(); student2.setName("小米"); student2.setAge(18); student2.setSex("男"); student2.setMail("123.qq.com"); student2.setAddress("北京"); student2.setBirthday(getDate()); list.add(student1); list.add(student2); //list集合,序列化json格式的字符串 String str = JSON.toJSONString(list); //转换后的结果是数组,数组的元素是对象 // [{"address":"北京","age":18,"birthday":1714543419605,"mail":"123.qq.com","name":"小米","sex":"男"}, // {"address":"北京","age":18,"birthday":1714543419605,"mail":"123.qq.com","name":"小米","sex":"男"}] System.out.println(str); } @Test //java对象,序列化json格式的字符串 public void testObjectToJson(){ Student student = new Student(); student.setName("小米"); student.setAge(18); student.setSex("男"); student.setMail("123.qq.com"); student.setAddress("北京"); student.setBirthday(getDate()); String str = JSON.toJSONString(student); // {"address":"北京","age":18,"mail":"123.qq.com","name":"小米","sex":"男"} // {"address":"北京","age":18,"birthday":1714561073390,"mail":"123.qq.com","name":"小米","sex":"男"} System.out.println(str); } }
json的序列化和反序列化
最新推荐文章于 2024-08-10 15:17:40 发布