开始使用免费开始使用

JSON 数据分析

产品目录已载入,市场团队需要一份定价报告。他们想了解平均价格,找出最贵的产品,并查看产品在各个类别中的分布。

好消息是:一旦将 JSON 数据读入 Tablesaw 表,您就可以复用已经熟悉的列操作。

JsonReaderJsonReadOptionsTable 类都已导入,products.json 文件包含 namepricecategory 列。

本练习是课程的一部分

Java 中的数据导入

查看课程

练习说明

  • 完成 JSON 读取以载入 products 表。
  • 使用列方法计算平均价格。
  • 找出数据集中最高的价格。

交互式实操练习

通过完成这段示例代码来试试这个练习。

public class JSONAnalysis {
    public static void main(String[] args) {
        // Complete the JSON read
        JsonReadOptions options = JsonReadOptions.builder("products.json").build();
        Table products = new JsonReader().____(options);
        
        // Calculate average price
        double avgPrice = products.doubleColumn("price").____();
        
        // Find maximum price
        DoubleColumn priceCol = products.doubleColumn("price");
        double maxPrice = ____.____();
        String mostExpensive = products
            .where(priceCol.isEqualTo(maxPrice))
            .stringColumn("name").get(0);
        
        Table categoryCount = products.countBy("category");
        System.out.println("Product Analytics Report:");
        System.out.println("========================");
        System.out.printf("Average Price: $%.2f%n", avgPrice);
        System.out.println("Most Expensive: " + mostExpensive + " ($" + maxPrice + ")");
        System.out.println("\nCategory Distribution:");
        System.out.println(categoryCount.print());
    }
}
编辑并运行代码