【问题标题】:Reading an inner object with Circe用 Circe 读取内部对象
【发布时间】:2017-04-22 06:02:28
【问题描述】:

我正在尝试读取我收到的 json 中的内部对象。我需要按原样获取这个内部对象并将其插入 Mongo。

{
  "order" : {
    "customer" : {
      "name" : "Custy McCustomer",
      "contactDetails" : {
        "address" : "1 Fake Street, London, England",
        "phone" : "0123-456-789"
      }
    },
    "items" : [
      {
        "id" : 123,
        "description" : "banana",
        "quantity" : 1
      },
      {
        "id" : 456,
        "description" : "apple",
        "quantity" : 2
      }
    ],
    "total" : 123.45
  }
}

【问题讨论】:

  • 你指的是什么内部对象?
  • 是的,很抱歉。在此示例中,它将是客户或项目
  • 等等...这是 circe optics 文档页面上的示例?
  • 是的。我显然必须用另一个 json 来做这个,但这是同样的问题。

标签: scala circe


【解决方案1】:

基于原始示例,您将推出Decoder。我不是circe专家,我昨天才第一次使用它,但我认为downField应该可以。

case class Item(id: String, description: String, quantity: Int)
case class InnerObject(items: List[Item])

object InnerObject {
  implicit val decode: Decoder[InnerObject] = Decoder.instance(c =>
    c.downField("items").as[InnerObject]
  )
}

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多