Java 中的数据导入
Anthony Markham
VP Quant Developer

| Name | Age | City |
|-------|-----|---------|
| Alice | 30 | Boston |
| Bob | 25 | Seattle |
[
{"name": "Alice",
"age": 30,
"address": {"city": "Boston",
"state": "MA"}},
{"name": "Bob",
"age": 25,
"address": {"city": "Seattle",
"state": "WA"}}
]
// 读取 JSON 的简单方法
Table products = Table.read().file("products.json");
$$
import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
// 使用 JsonReadOptions 读取 JSON 文件 JsonReadOptions options = JsonReadOptions.builder("products.json").build();Table products = new JsonReader().read(options);
Tablesaw 方法// 从 JSON 数据访问列 StringColumn names = table.stringColumn("name"); DoubleColumn prices = table.doubleColumn("price");// 执行计算 double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
// 验证与错误处理
try {
JsonReadOptions options = JsonReadOptions.builder("data.json").build();
Table data = new JsonReader().read(options);
if (data.rowCount() > 0) {
// 处理数据
}
} catch (Exception e) {
System.err.println("Error reading JSON: " + e.getMessage());
}
if (data.rowCount() == 100) {
System.out.println("Table has exactly 100 rows - processing data");
}
// 删除含任意缺失值的行
data = data.dropRowsWithMissingValues();
// 将整数列转为 double 便于计算
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Java 中的数据导入