JSON簡介(java中的json庫使用)

清風輓歌發表於2020-11-21

JSON資料格式

JSON(JavaScript Object Notation)js物件標記語言,是一種輕量級的資料交換格式,他是基於ECMAScript的一個子集,採用完全獨立於程式語言的文字格式來儲存表示資料,簡潔和清晰的層次結構使得JSON成為理想的資料交換語言,易於人閱讀和編寫,同樣也易於機器的解析和生成,並且有效的提升網路傳輸的效率

官網:https://www.json.org/json-zh.html

JSON的三種資料格式:

型別語法說明
物件型別{key1:value,key2:value···}key是字串型別,value型別任意
陣列/集合型別[value1,value2,value3···]把它當做成一個陣列,value型別任意
混合型別[{key1:value···},{key2:value···}]陣列中放物件
{key1:[value1,value2111],key2[]···}物件中放陣列
合理包裹巢狀物件型別和陣列型別

JSON轉換工具:

json的轉換工具是通過java封裝好的一些jar工具包,直接將java物件或集合轉換成json格式的字串。

工具名稱說明
jsonlibjava類庫,需要匯入的jar包較多
GsonGoogle提供的一個json轉換工具
fastjson阿里巴巴提供的json轉換工具,效能較高
jackson知名、好用的json庫,Spring預設使用的JSON\XML解析器

jackson

使用步驟:匯入jar包,建立建立json轉換物件,呼叫物件中的方法,進行json格式和物件之間的相互轉換

		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-databind</artifactId>
		  <version>2.11.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-core</artifactId>
		  <version>2.11.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-annotations</artifactId>
		  <version>2.11.3</version>
		</dependency>

兩個常用類:

  • ObjectMapper:是jackson類庫中的主要類,他提供了將java物件和json格式字串相互轉換的功能,序列化和反序列化
  • TypeReference:用來指定反序列化的型別

物件型別轉換:

    @Test
    public void test1() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = new Student(1001,"小明",20);
        //將物件轉換為json格式字串
        String jsonStr = objectMapper.writeValueAsString(student);
        System.out.println(jsonStr);//返回結果:{"id":1001,"name":"小明","age":20}
        //再將json字串轉換為物件
        Student student1 = objectMapper.readValue(jsonStr,Student.class);
        System.out.println(student1.getName());
    }

集合型別的轉換:

list:

    @Test
    public void test3() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001,"小明",20));
        list.add(new Student(1002,"小紅",22));
        list.add(new Student(1003,"小強",21));
        String jsonStr = objectMapper.writeValueAsString(list);
        //[{"id":1001,"name":"小明","age":20},{"id":1002,"name":"小紅","age":22},{"id":1003,"name":"小強","age":21}]
        System.out.println(jsonStr);

        List<Student> list1 = objectMapper.readValue(jsonStr, new TypeReference<>() {});
        System.out.println(list1.get(0));

    }

map:

    @Test
    public void test4() throws JsonProcessingException {
        Map<String,Student> map = new HashMap<>();
        map.put("stu1",new Student(1001,"小明",20));
        map.put("stu2",new Student(1002,"小紅",21));
        map.put("stu3",new Student(1003,"小強",22));
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(map);
        //{"stu2":{"id":1002,"name":"小紅","age":21},"stu3":{"id":1003,"name":"小強","age":22},"stu1":{"id":1001,"name":"小明","age":20}}
        System.out.println(jsonStr);

        Map<String,Student> map1 = objectMapper.readValue(jsonStr, new TypeReference<Map<String, Student>>() {});
        System.out.println(map1.get("stu1"));
    }

fastjson

依賴包

		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
		  <groupId>com.alibaba</groupId>
		  <artifactId>fastjson</artifactId>
		  <version>1.2.75</version>
		</dependency>

常用方法:

  • JSON.toJSONString(obj) 將一個物件序列化為一個json格式的字串
  • JSON.parseObject(jsonStr,obj.class) 將一個json格式的字串反序列化為指定物件型別

fastjson中也提供了TypeReference類

示例:

    @Test
    public void test4(){
        Student student = new Student(1001,"小明",20);
        
        String jsonStr = JSON.toJSONString(student);
        System.out.println(jsonStr);//返回結果:{"id":1001,"name":"小明","age":20}
        
        //再將json字串轉換為物件
        Student student1 = JSON.parseObject(jsonStr,Student.class);
        System.out.println(student1.getName());
    }


    @Test
    public void test5() {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001,"小明",20));
        list.add(new Student(1002,"小紅",22));
        list.add(new Student(1003,"小強",21));
        
        String jsonStr = JSON.toJSONString(list);
        //[{"id":1001,"name":"小明","age":20},{"id":1002,"name":"小紅","age":22},{"id":1003,"name":"小強","age":21}]
        System.out.println(jsonStr);

        List<Student> list1 = JSON.parseObject(jsonStr, new TypeReference<>(){});
        System.out.println(list1.get(0));
    }

    @Test
    public void test6(){
        Map<String,Student> map = new HashMap<>();
        map.put("stu1",new Student(1001,"小明",20));
        map.put("stu2",new Student(1002,"小紅",21));
        map.put("stu3",new Student(1003,"小強",22));

        String jsonStr = JSON.toJSONString(map);
        //{"stu2":{"id":1002,"name":"小紅","age":21},"stu3":{"id":1003,"name":"小強","age":22},"stu1":{"id":1001,"name":"小明","age":20}}
        System.out.println(jsonStr);

        Map<String,Student> map1 = JSON.parseObject(jsonStr,new TypeReference<>(){});
        System.out.println(map1.get("stu1"));
    }

相關文章