進階 JSON 處理

Java 中的資料匯入

Anthony Markham

VP Quant Developer

複雜 JSON 資料

  • 巢狀物件與陣列
  • 混合資料型別
  • 處理 API 與設定檔 🧠

$$

複雜的 json 資料

Java 中的資料匯入

巢狀 JSON 物件

  • 巢狀:物件裡面再包物件
  • Tablesaw 可自動攤平簡單情況
  • 攤平:把每列中含清單或陣列的欄轉成多列
{
  "customer": {
    "name": "John Doe",
    "address": {
      "street": "123 Main St",                            <- 巢狀資訊
      "city": "Boston",                                   <- 巢狀資訊
      "coordinates": {"lat": 42.3601, "lng": -71.0589}    <- 巢中之巢的資訊
    }
  }
}
Java 中的資料匯入

JSON 攤平

customer.name customer.address.street customer.address.city
John Doe 123 Main St Boston

$ $

customer.address.coordinates.lat customer.address.coordinates.lng
42.3601 -71.0589
Java 中的資料匯入

JsonReader 設定

  • JsonReadOptions 可設定:
    • 資料來源(檔案、URL,或字串)
    • 表格名稱設定
    • 遺漏值處理
JsonReadOptions options = JsonReadOptions

.builder("complex.json")
.tableName("Products")
.missingValueIndicator("N/A")
.build();
Table data = new JsonReader().read(options);
missingValues = data.stringColumn("name").isMissing();
Java 中的資料匯入

表格連接(Join)

  • joinOn 把表格連接起來
  • Inner join 僅保留兩表皆存在的列
Table phones = Table.read().csv("phones.csv");   // name, phone

Table diets = Table.read().csv("diets.csv"); // name, diet
// Perform the inner join on the two tables Table joined = phones.joinOn("name").inner(diets);

$$

$$

  • 也有許多其他連接類型
Java 中的資料匯入

一起來練習吧!

Java 中的資料匯入

Preparing Video For Download...