Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- yml
- 1*1000
- AuditingEntityListener
- MSSQL
- 오블완
- java jdbc utf8mb4 연결 오류
- mysql equal null
- RootGraph
- createEntityGraph
- 버전 문자열 비교
- +9:00
- spring boot
- apatch poi
- load order
- getDateCellValue
- 운동해서 광명찾자
- sendFractionalSeconds
- NamedEntityGraph
- jdbc characterencoding utf8mb4
- mysql =
- mysql not equal null
- MYSQL
- https
- getEntityGraph
- mysql8업그레이드
- 운영체제별 차이
- Protecle
- jdbc utf8mb4
- 티스토리챌린지
- fractional seconds
Archives
- Today
- Total
Hello
2019.06.06 본문
728x90
자바 객체 -> json (직렬화)
@Test public void test() { Map<String, String> testMap = new HashMap<>(); testMap.put("phone", "iphone8plus"); testMap.put("netbook", "bossmonster"); testMap.put("tv", "lg"); Computer computer = new Computer("MSI",16,"i7-8700k","RTX 2060 페가수스 D6 6GB"); ObjectMapper objectMapper = new ObjectMapper(); try { String testMapToJsonString = objectMapper.writeValueAsString(testMap); String computerToJsontring = objectMapper.writeValueAsString(computer); log.debug("test Map json => {}", testMapToJsonString); log.debug("computer json => {}", computerToJsontring); } catch (JsonProcessingException e) { log.debug("err massege =>{}",e.getMessage()); } } |
objectMapper.writeValueAsString을 통해 json형태의 string을 만들 수 있다.
json -> 자바 객체
@Test public void test() { ObjectMapper objectMapper = new ObjectMapper(); try { String testMapToJsonString = "{\"netbook\":\"bossmonster\",\"tv\":\"lg\",\"phone\":\"iphone8plus\"}"; String computerToJsontring = "{\"brand\":\"MSI\",\"ram\":16,\"cpu\":\"i7-8700k\",\"graphicCard\":\"RTX 2060 페가수스 D6 6GB\"}"; Map<String, String> jsonToMap = objectMapper.readValue(testMapToJsonString, Map.class); Computer jsonToComputerObject = objectMapper.readValue(computerToJsontring, Computer.class); log.debug("map => {}",jsonToMap); log.debug("computer => {}",jsonToComputerObject); }catch (IOException e) { log.debug("err massege =>{}",e.getMessage()); } } |
objectMapper.readValue를 사용해 json String과 변환될 객체 클래스를 넘겨주면 해당 클래스에 맞게 데이터 매핑시켜준다.
728x90
'try' 카테고리의 다른 글
2019.09.01 (0) | 2019.09.01 |
---|---|
2019.08.12 (0) | 2019.08.12 |
2019.08.12 (0) | 2019.08.12 |
2019.08.04 (0) | 2019.08.04 |
2019.05.29 (0) | 2019.05.29 |