💡 JSON Object와 JSON Array의 타입을 반드시 구분하자. JSON Object (JSON 객체):
JSON Array (JSON 배열):
|
문자열(String)
숫자(Number)
불리언(Boolean)
객체(Object)
배열(List)
널(Null)
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
package ch02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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 responseCode = conn.getResponseCode();
System.out.println("response code : " + responseCode);
// 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.err.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
}
package ch02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
/**
* JSON Array 형태를 파싱 해보자.
*/
public class MyHttpAlbumListClient {
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");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
// 응답 코드 확인
int responseCode = conn.getResponseCode();
System.out.println("response code : " + responseCode);
// 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.err.println("-------------------------------");
// gson lib 활용
//Gson gson = new Gson();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// 하나의 JSON Object 형태 파싱
// Album albumDTO = gson.fromJson(buffer.toString(), Album.class);
// [{...},{...},{...}]
// Gson 제공하는 Type 이라는 데이터 타입을 활용할 수 있다.
//Json 배열 형태를 쉽게 파싱하는 방법 -> TypeToken 안에 List<T> 을 활용 한다.
Type albumType = new TypeToken<List<Album>>(){}.getType();
List<Album> albumList = gson.fromJson(buffer.toString(), albumType);
System.out.println(albumList.size());
for(Album a : albumList ) {
System.out.println(a.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
} // end of main
}
'Java > JAVA 3 - 유용한 클래스' 카테고리의 다른 글
파싱이란 뭘까?(JSON 파싱 ) - 31 (0) | 2024.06.07 |
---|---|
공공 데이터 포탈 사용해 보기 - 30 (0) | 2024.06.04 |
순수 자바코드로 HttpServer 만들기 - 29 (2) | 2024.06.03 |
소켓을 활용한 HTTP 통신이란 - 28 (1) | 2024.06.03 |
제네릭(Generic) - 27 (1) | 2024.06.03 |