티스토리 뷰

 

String to ListMap
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public List<Map<String, Object>> getCommonList(String path) {
        List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();

        try {
            URL url = new URL(url);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(20000); //서버에 연결되는 Timeout 시간 설정
            con.setReadTimeout(60000); // InputStream 읽어 오는 Timeout 시간 설정
            con.setRequestMethod("GET");
            con.setDoOutput(false);

            StringBuilder sb = new StringBuilder();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream(), "utf-8"))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                }
                returnList = new ObjectMapper().readValue(sb.toString(), new TypeReference<List<Map<String, Object>>>(){});
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnList;
    }

 

String to Map
public Map<String, Object> jsonToMap(String result) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> returnMap = mapper.readValue(result, Map.class);
        return returnMap;
    }

 

String to JSON
JSONParser jsonParse = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParse.parse(code); //code는 json형식을 가지고 있는 string타입
String token = (String) jsonObj.get("access_token");

 

String to int
String no = "122";
int a = Integer.parseInt(no);

 

int to String
int a = 1;
String b = Integer.toString(a);

 

char to String
char str = total.charAt(i);
String change = String.valueOf(str);

 

 

댓글
링크
최근에 올라온 글
Total
Today
Yesterday