从 JInts 列表的 JArray 中提取单个值

Extract individual values from JArray of Lists of JInts

我正在练习 Stripe-payment 上的 Scala 和 Akka Streams API。我希望从 Json 响应中提取交易日期:created

这是响应(显示 2 笔交易):

{
  "object": "list",
  "data": [
    {
      "id": "txn_1Fqdyl2eZvKYlo2CXfUAnz1z",
      "object": "balance_transaction",
      "amount": 10000,
      "available_on": 1577145600,
      "created": 1576581159,
      "currency": "usd",
      "description": null,
      "exchange_rate": null,
      "fee": 320,
      "fee_details": [
        {
          "amount": 320,
          "application": null,
          "currency": "usd",
          "description": "Stripe processing fees",
          "type": "stripe_fee"
        }
      ],
      "net": 9680,
      "source": "ch_1Fqdyk2eZvKYlo2CwjSNI1vO",
      "status": "available",
      "type": "charge"
    },
    {
      "id": "txn_1Fqdyk2eZvKYlo2C7MuBhLpe",
      "object": "balance_transaction",
      "amount": 2000,
      "available_on": 1577145600,
      "created": 1576581158,
      "currency": "usd",
      "description": "テスト支払い",
      "exchange_rate": null,
      "fee": 88,
      "fee_details": [
        {
          "amount": 88,
          "application": null,
          "currency": "usd",
          "description": "Stripe processing fees",
          "type": "stripe_fee"
        }
      ],
      "net": 1912,
      "source": "ch_1Fqdyk2eZvKYlo2Ccg96i1QQ",
      "status": "available",
      "type": "charge"
    }
  ],
  "has_more": true,
  "url": "/v1/balance_transactions"
}

它可以从 1 到 100 个交易不等。到目前为止,我已经能够在只有 1 笔交易时提取我需要的价值。当有多个结果时,我需要能够提取值。目前该值被硬编码为:case Some(JArray(List(JInt(int)))).

我的代码:

 def epochToDate(epochMillis: BigInt): String = {
    val convertedToLong = epochMillis * 1000L
    val df:SimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
    df.format(convertedToLong)
  }

  val stripe = new Stream("txn_1Fqdyl2eZvKYlo2Cn0OSmwaY", "starting_after", hasMoreFromJson, 1, idFromJson)

  stripe.asSource().runForeach(id => {

    val rawValue = (parse(id) \ "data" \ "created").toOption
    rawValue match {

      case Some(JArray(List(JInt(int)))) => println("Date: " + epochToDate(int))
      case _ => println("Value not present in JSON")
    }
  })

尝试

val rawValue = (parse(id) \ "data" \ "created").toOption
rawValue match {
  case createdObjOpt: Option[JObject] =>
    createdObjOpt match {
      case Some(createdObj) =>
        createdObj.obj.foreach { created =>
          println(created)
        }
      case None => println("oh no!")
    }
  case _ => println("Value not present in JSON")
}