728x90
์ํฉ 1 - String ํ์ ์ List์ธ ๊ฒฝ์ฐ
"๋ฆฌ์คํธ๋ช " : [
"์์1",
"์์2",
"์์3",
"์์4"
]
์์์ " "๋ ์๋ต๊ฐ๋ฅ
์์
public class Blog {
private String address;
private List<String> visitor = new ArrayList<>();
public List<String> getVisitor() {
return visitor;
}
public String getAddress() {
return address;
}
public Blog() {
}
}
ํ ์คํธ์ฝ๋
@SpringBootTest
class ObjectMapperTest {
private ObjectMapper objectMapper = new ObjectMapper();
private static final String DIRECT_STRING_LIST = """
{
"address" : "123",
"visitor" :
[
"ShinD",
"LIKE",
"SPRING",
"AND",
"HATE",
"C"
]
}
""";
@Test
@DisplayName("List<String> ๋ณํ")
void stringListTest() throws JsonProcessingException {
final Blog blog = objectMapper.readValue(DIRECT_STRING_LIST, Blog.class);
System.out.println(blog.getAddress());
for (int i = 0; i < blog.getVisitor().size(); i++) {
System.out.println(blog.getVisitor().get(i));
}
}
}
์ํฉ 2 - Wrapper Class List์ธ ๊ฒฝ์ฐ
"๋ฆฌ์คํธ๋ช " : [
"์์1",
"์์2",
"์์3",
"์์4"
]
์์์ ""๋ ์๋ต๊ฐ๋ฅ
์์
public class Blog {
private String address;
private List<Integer> visitor = new ArrayList<>();
public List<Integer> getVisitor() {
return visitor;
}
public String getAddress() {
return address;
}
public Blog() {
}
}
ํ ์คํธ์ฝ๋
@SpringBootTest
class ObjectMapperTest {
private ObjectMapper objectMapper = new ObjectMapper();
private static final String DIRECT_INTEGER_LIST = """
{
"address" : "123",
"visitor" :
[
11,
22,
"33",
"44",
"55",
"66"
]
}
""";
@Test
@DisplayName("List<INTEGER> ๋ณํ")
void stringListTest() throws JsonProcessingException {
final Blog blog = objectMapper.readValue(DIRECT_INTEGER_LIST, Blog.class);
System.out.println(blog.getAddress());
for (int i = 0; i < blog.getVisitor().size(); i++) {
System.out.println(blog.getVisitor().get(i));
}
}
}
์ํฉ 3 - ์ด์ธ ๋ค๋ฅธ Class์ List์ธ ๊ฒฝ์ฐ
"๋ฆฌ์คํธ๋ช " : [
{"์ฒซ๋ฒ์งธ ์์ ํ๋๋ช 1" : "๊ฐ1" , "ํ๋๋ช 2" : "๊ฐ2" },
{"๋๋ฒ์งธ ์์ ํ๋๋ช 1" : "๊ฐ1" , "ํ๋๋ช 2" : "๊ฐ2" },
{"์ธ๋ฒ์งธ ์์ ํ๋๋ช 1" : "๊ฐ1" , "ํ๋๋ช 2" : "๊ฐ2" }
]
์์
public class Blog {
private String address;
private List<Student> visitor = new ArrayList<>();
public List<Student> getVisitor() {
return visitor;
}
public String getAddress() {
return address;
}
public Blog() {
}
}
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
ํ ์คํธ์ฝ๋
@SpringBootTest
class ObjectMapperTest {
private ObjectMapper objectMapper = new ObjectMapper();
private static final String CLASS_LIST = """
{
"address" : "123",
"visitor" :
[
{"name" : "ShinD", "age":"11"},
{"name" : "ShinA", "age":"22"},
{"name" : "ShinB", "age":"33"},
{"name" : "ShinC", "age":"44"}
]
}
""";
@Test
@DisplayName("List<Student> ๋ณํ")
void stringListTest() throws JsonProcessingException {
final Blog blog = objectMapper.readValue(CLASS_LIST, Blog.class);
System.out.println(blog.getAddress());
for (int i = 0; i < blog.getVisitor().size(); i++) {
System.out.println(blog.getVisitor().get(i).getName());
System.out.println(blog.getVisitor().get(i).getAge());
}
}
}
728x90