dependency 찾는 법 -> https://mvnrepository.com/search?q=object+mapper
maven Repository 가서 사용할 것 찾음
온라인에서 라이브러리 찾아서 자동으로 추가
dependencies {
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
{
"name": "홍길동",
"age": 10,
"cars": [
{
"name": "K5",
"carNumber": "11가 1111",
"type": "sedan"
},
{
"name": "Q5",
"carNumber": "22가 2222",
"type": "SUV"
}
]
}
package dto;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Car {
private String name;
@JsonProperty("car_number")
private String carNumber;
@JsonProperty("TYPE")
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", carNumber='" + carNumber + '\'' +
", type='" + type + '\'' +
'}';
}
}
package dto;
import java.util.List;
public class User {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
}
package org.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dto.Car;
import dto.User;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
System.out.println("main");
// 맥은 디폴트가 UTF-8이라 에러가 안나지만 윈도우는 남
ObjectMapper objectMapper = new ObjectMapper();
User user = new User();
user.setName("홍길동");
user.setAge(10);
Car car1 = new Car();
car1.setName("K5");
car1.setCarNumber("11가 1111");
car1.setType("sedan");
Car car2 = new Car();
car2.setName("Q5");
car2.setCarNumber("22가 2222");
car2.setType("SUV");
List<Car> carList = Arrays.asList(car1, car2);
user.setCars(carList);
System.out.println(user);
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
//순수 노드에 접근하는 법
JsonNode jsonNode = objectMapper.readTree(json);
// 파싱
String _name = jsonNode.get("name").asText();
int _age = jsonNode.get("age").asInt();
System.out.println("name :" + _name);
System.out.println("age :" + _age);
// 아무 것도 안 찍힘
// String _list = jsonNode.get("cars").asText();
// System.out.println(_list);
//cars는 일종의 노드
JsonNode cars = jsonNode.get("cars");
ArrayNode arrayNode = (ArrayNode)cars;
List<Car> _cars = objectMapper.convertValue(arrayNode, new TypeReference<List<Car>>() {});
System.out.println(_cars);
// JSONNODE 값 바꾸는법 put을 통해 벨류 값 바꾸었음
ObjectNode objectNode = (ObjectNode) jsonNode;
objectNode.put("name", "steve");
objectNode.put("age", 20);
System.out.println(objectNode.toPrettyString());
}
}