Noțiuni de bază JSON cu Tablesaw

Importul datelor în Java

Anthony Markham

VP Quant Developer

Introducere în JSON

  • JSON: JavaScript Object Notation
    • Format ușor pentru schimb de date
    • Format standard pentru API-uri web
    • Structură cheie-valoare

Imagine JSON

Importul datelor în Java

Date tabulare

  • Rânduri și coloane fixe
  • Tabular: tipuri de date uniforme
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Boston  |
| Bob   | 25  | Seattle |

JSON

  • Structuri imbricate flexibile
  • Tipuri de date și ierarhii mixte
[
  {"name": "Alice", 
      "age": 30, 
      "address": {"city": "Boston", 
                  "state": "MA"}},
  {"name": "Bob", 
      "age": 25, 
      "address": {"city": "Seattle", 
                  "state": "WA"}}
]
Importul datelor în Java

Citirea fișierelor JSON

  • Utilizați doar pentru fișiere JSON simple
// Simple method to read a JSON file
Table products = Table.read().file("products.json");

$$

import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
  • Oferă configurare suplimentară
// Reading a JSON file using JsonReadOptions
JsonReadOptions options = JsonReadOptions.builder("products.json").build();

Table products = new JsonReader().read(options);
Importul datelor în Java

Accesarea datelor JSON

  • Metodele Tablesaw familiare funcționează
// Access columns from JSON data
StringColumn names = table.stringColumn("name");
DoubleColumn prices = table.doubleColumn("price");

// Perform calculations double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
Importul datelor în Java

Bune practici JSON - validare

  • Validați structura JSON înainte de procesare
// Validation and error handling
try {
    JsonReadOptions options = JsonReadOptions.builder("data.json").build();
    Table data = new JsonReader().read(options);
    if (data.rowCount() > 0) {
        // Process data
    }
} 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");
}
Importul datelor în Java

Bune practici JSON - valori lipsă

  • Gestionați valorile lipsă/nule
// Remove rows with any missing values
data = data.dropRowsWithMissingValues();
  • Potriviți tipurile de date cu nevoile analizei
// Convert integer column to double for calculations
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Importul datelor în Java

Să exersăm!

Importul datelor în Java

Preparing Video For Download...