티스토리 뷰
Development/JAVA
[JAVA] 형 변환 (String to Map, String to int, int to String..)
쥬리리리 2022. 4. 11. 10:06
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);
'Development > JAVA' 카테고리의 다른 글
[JAVA] @getter boolean 타입 호출 안될 때 (0) | 2023.01.27 |
---|---|
[JAVA] ListMap for문, Map null 체크 (0) | 2022.04.22 |
[JAVA] Http POST urlencoded (0) | 2022.04.11 |
[JAVA] json to int (0) | 2022.01.05 |
[JAVA] spring boot + security 애플 로그인 구현 (0) | 2021.11.12 |
댓글
링크
최근에 올라온 글
- Total
- Today
- Yesterday