JSON 유효성 검사
제품 분석이 잘 작동하고 있어서 팀은 이를 자동 일일 보고서로 배포하려고 합니다. 하지만 운영 환경에서는 JSON 파일이 없거나 손상되었거나 예기치 않은 형식으로 도착할 수 있어요. 실제 운영에 들어가기 전에, 애플리케이션이 안전하게 종료되도록 오류 처리를 추가해야 합니다.
JsonReader, JsonReadOptions, Table 클래스는 이미 가져와 두었습니다.
이 연습은 강의의 일부입니다
Java에서 데이터 가져오기
연습 안내
- JSON을 로드하는 코드를 try 블록으로 감싸세요.
- 예외를 처리하는 catch 블록을 추가하세요.
- 로드에 실패하면 오류 메시지를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class JSONValidation {
public static void main(String[] args) {
// Add try block for JSON loading
____ {
JsonReadOptions options = JsonReadOptions.builder("products.json").build();
Table products = new JsonReader().read(options);
System.out.println("Successfully loaded " + products.rowCount() + " products");
System.out.println(products.first(3));
// Add catch block for exceptions
} ____ (Exception e) {
// Print error message
System.err.println("Error reading JSON: " + e.____());
}
}
}