Những điều cơ bản về dữ liệu JSON với Tablesaw

Nhập dữ liệu trong Java

Anthony Markham

VP Quant Developer

Giới thiệu JSON

  • JSON: JavaScript Object Notation
    • Định dạng trao đổi dữ liệu nhẹ
    • Chuẩn phổ biến cho Web API
    • Cấu trúc cặp khóa-giá trị

Hình minh họa JSON

Nhập dữ liệu trong Java

Dữ liệu dạng bảng

  • Hàng và cột cố định
  • Dạng bảng: Kiểu dữ liệu đồng nhất
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Boston  |
| Bob   | 25  | Seattle |

JSON

  • Cấu trúc lồng nhau linh hoạt
  • Trộn kiểu dữ liệu và phân cấp
[
  {"name": "Alice", 
      "age": 30, 
      "address": {"city": "Boston", 
                  "state": "MA"}},
  {"name": "Bob", 
      "age": 25, 
      "address": {"city": "Seattle", 
                  "state": "WA"}}
]
Nhập dữ liệu trong Java

Đọc JSON

  • Chỉ dùng cho file JSON đơn giản
// 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;
  • Cung cấp thêm tùy chọn cấu hình
// Reading a JSON file using JsonReadOptions
JsonReadOptions options = JsonReadOptions.builder("products.json").build();

Table products = new JsonReader().read(options);
Nhập dữ liệu trong Java

Truy cập dữ liệu JSON

  • Có thể dùng các phương thức quen thuộc của Tablesaw
// 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);
Nhập dữ liệu trong Java

Thực hành tốt với JSON - xác thực

  • Xác thực cấu trúc JSON trước khi xử lý
// 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");
}
Nhập dữ liệu trong Java

Thực hành tốt với JSON - giá trị thiếu

  • Xử lý giá trị thiếu/null
// Remove rows with any missing values
data = data.dropRowsWithMissingValues();
  • Khớp kiểu dữ liệu với nhu cầu phân tích
// Convert integer column to double for calculations
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Nhập dữ liệu trong Java

Cùng luyện tập nào!

Nhập dữ liệu trong Java

Preparing Video For Download...