JAVA Json to POJO Class

By | 2022년 7월 15일
Table of Contents

JAVA Json to POJO Class

클래스 생성

여기 에서 json 문자열을 이용해 POJO class 를 자동생성할 수 있다.

의존성 추가

dependencies {
    implementation "com.fasterxml.jackson.core:jackson-databind:2.13.3"
    implementation "com.fasterxml.jackson.core:jackson-core:2.13.3"
}

문자열에서 변환

ObjectMapper om = new ObjectMapper();
ItemDocument itemDocument = om.readValue(myJsonString, ItemDocument.class);

Object 에서 변환

ObjectMapper om = new ObjectMapper();

JSONParser parser = new JSONParser();
JSONObject jsonObj = (JSONObject) parser.parse(resultJson);

JSONArray results = (JSONArray) jsonObj.get("results");
for (Object obj : results) {
    ItemDocument itemDocument = om.convertValue(obj, ItemDocument.class);
    logger.debug("11111111111 : {}", itemDocument.getItemName());
}

답글 남기기