본문 바로가기

old

[JAVA] JSON 형식 String to Map 형변환

import org.codehaus.jackson.type.TypeReference;
import com.clipsoft.org.json.simple.parser.JSONParser;
import com.clipsoft.org.json.simple.parser.ParseException;

.
.
.
public Map<String, Object> stringToMap(Map<String, Object> param){
    // 1. Json 형식으로 받아온 String
    String response = stringBuffer.toString();
    System.out.println(response); // {"RESULT_SUMRY":{"CUR_PAGE":1,"TOTCNT":2252},"RESULT_LIST":[{"MBR_SEXDS..


    // 2.Parser
    JSONParser jsonParser = new JSONParser();

    // 3. To Object
    Object obj = null;
    try {
        obj = jsonParser.parse(response);
    } catch (ParseException e) {
        log.error("JSON parsing 에러 :: " + e);
    }

    // 4. To Map
    ObjectMapper objectMapper = new ObjectMapper();
    TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String,Object>>() {};

    Map<String, Object> returnData =  objectMapper.readValue(response, typeReference);
    return returnData;
}