Importare dati in Java
Anthony Markham
VP Quant Developer

| Nome | Età | Città |
|-------|-----|---------|
| Alice | 30 | Boston |
| Bob | 25 | Seattle |
[
{"name": "Alice",
"age": 30,
"address": {"city": "Boston",
"state": "MA"}},
{"name": "Bob",
"age": 25,
"address": {"city": "Seattle",
"state": "WA"}}
]
// Metodo semplice per leggere un file JSON
Table products = Table.read().file("products.json");
$$
import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
// Lettura di un file JSON usando JsonReadOptions JsonReadOptions options = JsonReadOptions.builder("products.json").build();Table products = new JsonReader().read(options);
Tablesaw familiari funzionano// Accedi alle colonne dai dati JSON StringColumn names = table.stringColumn("name"); DoubleColumn prices = table.doubleColumn("price");// Esegui calcoli double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
// Validazione e gestione errori
try {
JsonReadOptions options = JsonReadOptions.builder("data.json").build();
Table data = new JsonReader().read(options);
if (data.rowCount() > 0) {
// Elabora dati
}
} catch (Exception e) {
System.err.println("Errore lettura JSON: " + e.getMessage());
}
if (data.rowCount() == 100) {
System.out.println("La tabella ha esattamente 100 righe - elaborazione dati");
}
// Rimuovi righe con valori mancanti
data = data.dropRowsWithMissingValues();
// Converti colonna intera in double per calcoli
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Importare dati in Java