JAVA
JSON 파싱 연습
미로910
2024. 6. 7. 11:46
https://jsonplaceholder.typicode.com/
JSONPlaceholder - Free Fake REST API
{JSON} Placeholder Free fake and reliable API for testing and prototyping. Powered by JSON Server + LowDB. Serving ~3 billion requests each month.
jsonplaceholder.typicode.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class MyHttpAlbumClient {
public static void main(String[] args) {
// 순수 자바코드에서 HTTP 통신
// 1. 서버 주소 경로
// 2. URL 클래스
// 3. url.openConnection() <--- 스트림 I/O
try {
URL url = new URL("https://jsonplaceholder.typicode.com/albums/1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
// 응답 코드 확인
int respinseCode = conn.getResponseCode();
System.out.println("response code : " + respinseCode);
// HTTP 응답 메세지에 데이터를 추출 [] ---Stream--- []
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer buffer = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
buffer.append(inputLine);
}
in.close();
System.out.println(buffer.toString());
System.out.println("-------------------");
// gson lib 활용
// Gson gson = new Gson();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Album albumDTO = gson.fromJson(buffer.toString(), Album.class);
System.out.println(albumDTO.getId());
System.out.println(albumDTO.getUserId());
System.out.println(albumDTO.getTitle());
} catch (IOException e) {
e.printStackTrace();
}
}// end of main
}// end of class
실행 결과________