IT·인터넷

JSON 값 맵 형태로 바꾸기

ukidugi 2015. 7. 3.

오늘 일하면서 고급정보를 얻었다

바로 JSON 값을 맵 형태로 바꾸어 키값과 밸류값을 두가지 를 얻을 수 있다는 점이다.

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
CommonMap resultMap = new CommonMap();
        ObjectMapper objectMapper = new ObjectMapper();
        
        try {
            resultMap = (CommonMap)objectMapper.readValue(schedulerInfo, CommonMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(resultMap);
        
        Set a = resultMap.keySet();
        
        Iterator i = a.iterator();        
        while(i.hasNext()){
            //System.out.println(resultMap.get(i.next()));
            LinkedHashMap tmp = (LinkedHashMap)resultMap.get(i.next());
            CommonMap tmpResult = new CommonMap();
            String start_date = (String) tmp.get("start_date");
            String end_date = (String) tmp.get("end_date");
            
            tmpResult.put("start_date", CommonUtil.getCalendarDay(start_date));
            tmpResult.put("end_date", CommonUtil.getCalendarDay(end_date));
            tmpResult.put("text", tmp.get("text"));
            tmpResult.put("subject", tmp.get("subject"));
            int insertResult = service.insertResult(tmpResult);
cs



ObjectMapper objectMapper = new ObjectMapper();



ObjectMapper


Json 을 오브젝트로 만들어 준다는건데 원리는 잘 알겠지만 생각보다 어렵다...

암튼 키셋을 이용하면 키가 뽑히고 readVaue하면 밸류값이 뽑힌다.

나머지는 현재 프로젝트 명이므로 CommonMap 대신 HashMap을 쓰면 되겠다. 이상.



댓글