原文链接:这里
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);
}
}
|