Java中Jackson的用法


原文鏈接:這里
0.添加依賴
1
2
3
4
5
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.2</version>
</dependency>

1.實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.cat.domain;
 
public class Person {
     public  String name;
     public  String sex;
     public  int age;
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this.name = name;
     }
 
     public String getSex() {
         return sex;
     }
 
     public void setSex(String sex) {
         this.sex = sex;
     }
 
     public int getAge() {
         return age;
     }
 
     public void setAge(int age) {
         this.age = age;
     }
 
     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\'' +
                 ", sex='" + sex + '\'' +
                 ", age=" + age +
                 '}';
     }
}
2. list轉json

我們寫一個test類或者在Controller中直接構造,本文是使用的是直接在Controller中調用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.cat.controller;
 
import com.cat.domain.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@Controller
@RequestMapping (produces= "text/html;charset=UTF-8" )
public class IndexController {
     List<Person> PersonList  = new ArrayList<>();
     //返回json格式
     @RequestMapping (value = "/hello2" )
     @ResponseBody
     public Object hello2() throws IOException {
         Person Person4 = new Person();
         Person Person5 = new Person();
         Person Person6 = new Person();
         Person4.name = "王曉紅" ;
         Person5.name = "王曉綠" ;
         Person6.name = "王曉藍" ;
         Person4.age = 13 ;
         Person5.age = 14 ;
         Person6.age = 16 ;
         Person4.sex = "男" ;
         Person5.sex = "女" ;
         Person6.sex = "男" ;
         PersonList.add(Person4);
         PersonList.add(Person5);
         PersonList.add(Person6);
         System.out.println(PersonList);
         ObjectMapper mapper = new ObjectMapper();
         String str = mapper.writeValueAsString(PersonList);
         System.out.println(str);
         return str;
     }
}

可以看出,控制台中輸出了兩端,第一段是list對象,第二段是json格式。



3.json轉對象(反序列化)

我們這次自己編寫一個類用來測試。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import com.cat.domain.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.List;
public class main {
 
     public static void main(String[] args) throws Exception {
         String json = "[{\"name\":\"王曉紅\",\"sex\":\"男\",\"age\":13},{\"name\":\"王曉綠\",\"sex\":\"女\",\"age\":14},{\"name\":\"王曉藍\",\"sex\":\"男\",\"age\":16}]" ;
         ObjectMapper mapper = new ObjectMapper();
 
         // json 轉數組對象
         Person[] person = mapper.readValue(json,Person[]. class );
         for (Person person1:person)
             System.out.println(person1);
     }
}


4.json轉List(反序列化)
1
2
3
4
5
6
7
8
9
10
11
12
13
import com.cat.domain.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.List;
public class main {
     public static void main(String[] args) throws Exception {
         String json = "[{\"name\":\"王曉紅\",\"sex\":\"男\",\"age\":13},{\"name\":\"王曉綠\",\"sex\":\"女\",\"age\":14},{\"name\":\"王曉藍\",\"sex\":\"男\",\"age\":16}]" ;
         ObjectMapper mapper = new ObjectMapper();
         // json 轉List對象
         List<Person> person2 =  Arrays.asList(mapper.readValue(json,Person[]. class ));
         System.out.println(person2);
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM