我试图在我收到的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
}
}
答案 0 :(得分:1)
根据原始示例,您将推出Decoder
。我不是一个专家,我昨天第一次使用它,但我认为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]
)
}