JSON validation
आपका प्रोडक्ट विश्लेषण ठीक चल रहा है, और टीम इसे एक स्वचालित दैनिक रिपोर्ट के रूप में डिप्लॉय करना चाहती है. लेकिन प्रोडक्शन में JSON फाइलें गायब हो सकती हैं, करप्ट हो सकती हैं, या अप्रत्याशित फ़ॉर्मैट में आ सकती हैं. लाइव जाने से पहले, आपको error handling जोड़नी है ताकि एप्लिकेशन गरिमापूर्वक फेल हो.
JsonReader, JsonReadOptions, और Table क्लास आपके लिए इम्पोर्ट कर दी गई हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में डेटा इम्पोर्ट करना
अभ्यास निर्देश
- JSON लोड करने वाले कोड को लपेटने के लिए एक try ब्लॉक जोड़ें.
- किसी भी exception को संभालने के लिए एक catch ब्लॉक जोड़ें.
- अगर लोडिंग फेल हो जाए तो error message प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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.____());
}
}
}