Extract Options from potentially null JSON values using for expression

后端 未结 4 1758
日久生厌
日久生厌 2021-01-19 02:09

I have a JSON document where some values can be null. Using for expressions in json4s, how I can yield None, instead of nothing?

The following will fail to yield whe

4条回答
  •  悲哀的现实
    2021-01-19 02:56

    The last command should look like:

    for {
      JObject(thing) <- res1
    } yield thing.collectFirst{case JField("PlanEstimate", JDouble(points)) => points}
    

    Or like

    for {
      JObject(thing) <- res1
      points = thing.collectFirst{case JField("PlanEstimate", JDouble(p)) => p}
    } yield points
    

提交回复
热议问题