开始使用免费开始使用

JSON 校验

您的产品分析运行良好,团队希望将其部署为每日自动报告。但在生产环境中,JSON 文件可能缺失、损坏,或以意外的格式到达。上线前,您需要添加错误处理,让应用能够平稳地处理失败情况。

JsonReaderJsonReadOptionsTable 类已为您导入。

本练习是课程的一部分

Java 中的数据导入

查看课程

练习说明

  • 添加一个 try 代码块来包裹 JSON 加载代码。
  • 添加一个 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.____());
        }
    }
}
编辑并运行代码