본문으로 바로가기

0. 환경

  • JDK 11(Java 11)
  • IntelliJ
  • jackson
  • lombok

[build.gradle dependencies]

dependencies {
   implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.1'
   compileOnly 'org.projectlombok:lombok'
}

1. jackson

jackson 라이브러리를 활용해서 매우 편하게 Json String을 DTO(VO)로 매핑할 수 있습니다.

 

[jackson]

1. ObjectMapper 객체 생성
ObjectMapper mapper = new ObjectMapper();

2. readValue() 메소드를 이용해 매핑합니다.
변환 DTO클래스 변환 DTO레퍼런스 = mapper.readValue(Json String 값, 변환 DTO.class);

2. Json String to DTO(VO) 1

{
	"ipAddr": "192.168.70.2",
	"macAddr": "AA:BB:CC:DD:EE:FF",
	"serial": "TESTER"
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

public class test {

    @Data
    static class Device {
        private String macAddr;
        private String serial;
        private String ipAddr;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String jsonString = "{\"ipAddr\":\"192.168.70.2\",\"macAddr\":\"AA:BB:CC:DD:EE:FF\",\"serial\":\"TESTER\"}";

        ObjectMapper mapper = new ObjectMapper();
        Device device = mapper.readValue(jsonString, Device.class);

        System.out.println(device.getIpAddr());
        System.out.println(device.getMacAddr());
        System.out.println(device.getSerial());
    }
}

3. Json String to DTO(VO) 2

{
	"device": {
		"ipAddr": "192.168.70.2",
		"macAddr": "12:12:12:12:12:12",
		"serial": "TESTER"
	}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

public class test {

    @Data
    static class Device {
        private String macAddr;
        private String serial;
        private String ipAddr;
    }

    @Data
    static class DeviceJsonName {
        private Device device;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String jsonString = "{\"device\":{\"ipAddr\":\"192.168.70.2\",\"macAddr\":\"12:12:12:12:12:12\",\"serial\":\"TESTER\"}}";

        ObjectMapper mapper = new ObjectMapper();
        DeviceJsonName deviceJsonName = mapper.readValue(jsonString, DeviceJsonName.class);

        System.out.println(deviceJsonName.getDevice().getIpAddr());
        System.out.println(deviceJsonName.getDevice().getMacAddr());
        System.out.println(deviceJsonName.getDevice().getSerial());
    }
}

4. Json Array String to DTO(VO) 1

[
  {
	"ipAddr": "192.168.70.1",
	"macAddr": "AA:BB:CC:DD:EE:FF"
  }, 
  {
	"ipAddr": "192.168.70.11",
	"macAddr": "AA:BB:CC:DD:EE:EE"
  }, 
  {
	"ipAddr": "192.168.70.7",
	"macAddr": "AB:BB:AB:DD:EE:FF"
  }
]
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

public class test {

    @Data
    static class Devices {
        private String ipAddr;
        private String macAddr;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String jsonString = "[{\"ipAddr\":\"192.168.70.1\",\"macAddr\":\"AA:BB:CC:DD:EE:FF\"},{\"ipAddr\":\"192.168.70.11\",\"macAddr\":\"AA:BB:CC:DD:EE:EE\"},{\"ipAddr\":\"192.168.70.7\",\"macAddr\":\"AB:BB:AB:DD:EE:FF\"}]";

        // class java.util.LinkedHashMap cannot be cast to class[ClassCastException]
//        ObjectMapper mapper = new ObjectMapper();
//        List<Devices> devices = mapper.readValue(jsonString, List.class);
//        devices.stream().forEach(d -> {
//            System.out.println(d.getIpAddr() + " : " + d.getMacAddr());
//        });
        
        ObjectMapper mapper = new ObjectMapper();
        List<Devices> devices = Arrays.asList(mapper.readValue(jsonString, Devices[].class));
        devices.stream().forEach(d -> {
            System.out.println(d.getIpAddr() + " : " + d.getMacAddr());
        });

    }
}

5. Json Array String to DTO(VO) 2

{
	"devices": [
	  {
		"ipAddr": "192.168.70.1",
		"macAddr": "AA:BB:CC:DD:EE:FF"
	  }, 
	  {
		"ipAddr": "192.168.70.11",
		"macAddr": "AA:BB:CC:DD:EE:EE"
	  }, 
	  {
		"ipAddr": "192.168.70.7",
		"macAddr": "AB:BB:AB:DD:EE:FF"
	  }
	]
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

public class test {

    @Data
    static class Devices {
        private String ipAddr;
        private String macAddr;
    }

    @Data
    static class DevicesJsonName {
        private List<Devices> devices;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String jsonString = "{\"devices\":[{\"ipAddr\":\"192.168.70.1\",\"macAddr\":\"AA:BB:CC:DD:EE:FF\"},{\"ipAddr\":\"192.168.70.11\",\"macAddr\":\"AA:BB:CC:DD:EE:EE\"},{\"ipAddr\":\"192.168.70.7\",\"macAddr\":\"AB:BB:AB:DD:EE:FF\"}]}";

        ObjectMapper mapper = new ObjectMapper();
        DevicesJsonName devicesJsonName = mapper.readValue(jsonString, DevicesJsonName.class);

        devicesJsonName.getDevices()
                .stream()
                .forEach(d -> {
                    System.out.println(d.getIpAddr() + " : " + d.getMacAddr());
                });
    }
}

6. Json String to DTO(VO)

{
	"ipAddr": "192.168.70.2",
	"macAddr": "AA:BB:CC:DD:EE:FF",
	"serial": "TESTER",
	"devices": [
	  {
		"ipAddr": "192.168.70.1",
		"macAddr": "AA:BB:CC:DD:EE:FF"
	  }, 
	  {
		"ipAddr": "192.168.70.11",
		"macAddr": "AA:BB:CC:DD:EE:EE"
	  }, 
	  {
		"ipAddr": "192.168.70.7",
		"macAddr": "AB:BB:AB:DD:EE:FF"
	  }
	]
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

public class test {

    @Data
    static class Devices {
        private String ipAddr;
        private String macAddr;
    }

    @Data
    static class DeviceInfo {
        private String ipAddr;
        private String macAddr;
        private String serial;
        private List<Devices> devices;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String jsonString = "{\"ipAddr\":\"192.168.70.2\",\"macAddr\":\"AA:BB:CC:DD:EE:FF\",\"serial\":\"TESTER\",\"devices\":[{\"ipAddr\":\"192.168.70.1\",\"macAddr\":\"AA:BB:CC:DD:EE:FF\"},{\"ipAddr\":\"192.168.70.11\",\"macAddr\":\"AA:BB:CC:DD:EE:EE\"},{\"ipAddr\":\"192.168.70.7\",\"macAddr\":\"AB:BB:AB:DD:EE:FF\"}]}";

        ObjectMapper mapper = new ObjectMapper();
        DeviceInfo deviceInfo = mapper.readValue(jsonString, DeviceInfo.class);

        System.out.println(deviceInfo.getIpAddr());
        System.out.println(deviceInfo.getMacAddr());
        System.out.println(deviceInfo.getSerial());
        deviceInfo.getDevices()
                .stream()
                .forEach(d -> {
                    System.out.println(d.getIpAddr() + " : " + d.getMacAddr());
                });
    }
}