반응형

 

Spring Boot에서 Json 파싱을 위해 Jackson을 기본적으로 제공해준다. Spring에서는 maven 또는 gradle을 xml에 추가해야한다.

사실 기본적인 기능은 앞선 gson과 다를게 없다.

 

Spring boot Json 1편, 2편, 4편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

1. Jackson ObjectMapper (JsonString to Object)

 

public void test() { 		ObjectMapper objectMapper = new ObjectMapper();  		String Json = 		    "{ \"id\" : 1, \"password\" : \"1234\" }";  		try { 		    TestDTO t = objectMapper.readValue(Json, TestDTO.class);	// String to Object로 변환  		    System.out.println("id = " + t.getId()); 		    System.out.println("password = " + t.getPassword()); 		     		    String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t); //다시 jsonString으로 변환 		    System.out.println(jsonString); 		} catch (IOException e) { 		    e.printStackTrace(); 		} }

 

(위의 TestDTO은 1편에 나와있다.)

 

objectMapper를 통해 JsonString을 Object로 변환할 수 있었고 writeValueAsString을 통해 다시 JsonString으로도 변환이 가능하다

반응형

2. Object From JsonFile

 

 

ObjectMapper objectMapper = new ObjectMapper();  URL url = new URL("file:data/test.json");  TestDTO t = objectMapper.readValue(url, TestDTO.class); 

 

test.json 파일엔

{

"id" : 1,

"password" : 1234

}

들어 가있다.

 

 

기본적인 사용방법은 여기까지이다 다음편에 Jackson JsonNodeClass를 설명한다.

Jackson 가이드 - https://www.baeldung.com/jackson

반응형

+ Recent posts