메뉴 건너뛰기

조회 수 1010 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

브라우저에서 위 URL 로 접속해 보면 JSON 문자열을 다음과 같다.

1
2
3
4
5
6
7
{
   type: "success",
   value: {
      id: 10,
      quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
   }
}



요것을 처리하는 예제 코드

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
44
45
46
47
48
49
50
51
52
53
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
 
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
public class JacksonTest {
    public static void main(String[] args)
            throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
 
        ObjectMapper mapper = new ObjectMapper();
 
        // URL 에 있는 JSON String 을 Map으로 변환하기
        Map<String, Object> data = mapper.readValue(
                     new URL("https://gturnquist-quoters.cfapps.io/api/random"),
                     new TypeReference<Map<String,Object>>(){});
 
 
 
 
        // {type=success, value={id=9, quote=So easy it is to switch container in #springboot.}}
        System.out.println(data);
 
        // {id=9, quote=So easy it is to switch container in #springboot.}
        System.out.println(data.get("value"));
 
 
 
 
 
 
        // Map을 JSON String 으로 변환
        // {"type":"success","value":{"id":9,"quote":"So easy it is to switch container in #springboot."}}
        System.out.println(mapper.writeValueAsString(data));
 
 
        // Map을 보기쉬운 JSON String 으로 변환
        /*
           {
              "type" : "success",
              "value" : {
                "id" : 9,
                "quote" : "So easy it is to switch container in #springboot."
              }
           }
        */
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data));
    }
}



List of Articles
번호 제목 날짜 조회 수
131 회원가입 폼(form)으로 보는 Validator 구현하는 방법 file 2016.08.29 7115
130 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3858
129 Database Connections 생성하기 (오라클) file 2016.08.29 4072
128 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
127 전자정부 표준프레임워크 설치하기 file 2016.08.29 4250
126 간단한 개인홈페이지 만들어보기 file 2016.08.29 4751
125 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
124 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
123 전자정부표준프레임워크 - 설치 file 2016.09.02 6306
122 Spring Boot 프로젝트 생성 file 2016.09.02 4160
121 전자정부프레임워크 v2.5, v2.6 오라클 세팅하기 file 2016.09.12 5109
120 JSP, Spring, GMail 메일발송 간단 예제 2016.09.12 32791
119 변수의 종류 2016.09.13 3980
118 클래스멤버와 인스턴스멤버간의 참조와 호출 2016.09.13 3346
117 클래스 메서드와 인스턴스 메서드 2016.09.13 3720
116 static 2016.09.13 3354
115 컬렉션 프레임워크 file 2016.09.13 3707
114 제네릭 file 2016.09.13 3297
113 파일I/O 개요 file 2016.09.13 3512
112 바이트 기반의 스트림 file 2016.09.13 3763
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved