Programing/Spring

Json / Jackson / JsonNode / ObjectNode

BeomJun.Kwon 2022. 3. 24. 16:30

 

Json / Jackson / JsonNode / ObjectNode

 

 

Arraynode : [ ] 표시로 시작되는 배열

JsonNode : { } 표시로 시작하는 Json 값

- JsonNode는 값을 불러올 수는 있지만 넣을 수는 없기 때문에 값을 넣기 위해서는 ObjectNode를 사용

- JsonNode는 값을 읽을 수만 있고 ObjectNode는 값을 읽고 쓸 수 있다.

 

 

JSON

- 개방형 표준 포맷, XML(AJAX 등) 사용

- Key-Value 사용

 

 

JACKSON

- Jackson은 JSON 데이터 구조를 처리해주는 라이브러리 입니다.

- ObjectMapper API를 사용, GSON or SimpeJSON과 같이 객체에 Data Setting
- Spring 3.0 이후로부터, Jacskon과 관련된 API를 제공

 

 

JSON ( JavaScript )

JSON.stringify : JSON 객체를 String 객체로 변환

JSON.parse() : String 객체를 JSON 객체로 변환

 

 

JACKSON ( JAVA )

public JsonNode readTree(String content) : readTree() 메소드는 Json 문자열을 받아서 JsonNode 객체를 리턴
public <T> T readValue(String content, Class<T> valueType) : readValue() 메소드는 2번째 파라미터로, Json문자열을 변환할 클래스 타입을 입력, Json 문자열을 JsonNode 등 다른 객체 타입으로도 변환

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonStringToObject {
    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        
        // {"id" : 1, "name" : "Anna"}
        String jsonStr = "{\"id\" : 1, \"name\" : \"Anna\"}";
        
        // jackson objectmapper 객체 생성
        ObjectMapper objectMapper = new ObjectMapper();
        
        // JSON String -> Student Object
        Student student = objectMapper.readValue(jsonStr, Student.class);
        
        // Student 객체 출력
        System.out.println(student);
        
    }
}

 

 

 

 

 

 

 

 

 


(참조) https://yonoo88.tistory.com/135
(참조) https://wikim.tistory.com/225

(참조) https://hianna.tistory.com/634

(참조) https://mommoo.tistory.com/83

'Programing > Spring' 카테고리의 다른 글

Spring-Boot와 호환되는 라이브러리 버전 정보 확인 방법  (0) 2023.10.24
Spring Integration  (0) 2022.03.25
RequestContextHolder  (0) 2022.03.21
XML Handler Mapping  (0) 2022.03.11
Spring Annotation 활성화  (0) 2022.03.11