JSON validation
Your product analysis is working well, and the team wants to deploy it as an automated daily report. But in production, JSON files might be missing, corrupted, or arrive in unexpected formats. Before going live, you need to add error handling so the application fails gracefully.
The JsonReader, JsonReadOptions, and Table classes have been imported for you.
Diese Übung ist Teil des Kurses
Importing Data in Java
Anleitung zur Übung
- Add a try block to wrap the JSON loading code.
- Add a catch block to handle any exceptions.
- Print the error message if loading fails.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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.____());
}
}
}