给出以下JSON结构
val rawJson =
"""
|{
| "users": [
| {
| "name": "Mario",
| "age": 10,
| "address": {
| "street": "13 Blvd",
| "postcode": "ABC 123"
| }
| },
| {
| "name": "Wario",
| "age": 70,
| "address": {
| "street": "22 Blvd",
| "postcode": "CBA 321"
| }
| }
| ]
|}
""".stripMargin
和相应的模型
case class Address(street: String, postcode: String)
case class User(name: String, age: Int, address: Address)
case class RequestBody(users: List[User])
circe可以自动反序列化
decode[RequestBody](rawJson))
通过像这样的结构类型定义模型时,可以自动进行反序列化
case class RequestBody(users: List[ {
val name: String
val age: Int
val address: {
val street: String
val postcode: String
}
}])