我有3個case類:創建讀取的列表[案例類]
scala> case class Friend(id: Long, hobbies: List[Long])
defined class Friend
scala> case class Kid(friends: List[Friend])
defined class Kid
scala> case class Parent(kids: List[Kid])
defined class Parent
它們可以顯示在層次結構(父母有孩子(一個或多個)的朋友(S)有(ID和愛好)。
Parent
---> Kid
---> Friend
---> id: Long
---> hobbies: List[Long]
我創建了FriendsReads
(儘管作爲senia指出here,我可能只需要使用
scala> implicit val FriendReads: Reads[Friend] = Json.format[Friend]
FriendReads: play.api.libs.json.Reads[Friend] = [email protected]
scala> implicit val FriendReads: Reads[Friend] = (
| (JsPath \ "id").read[Long] and
| (JsPath \ "hobbies").read[List[Long]]
|)(Friend.apply _)
FriendReads: play.api.libs.json.Reads[Friend] = [email protected]
然後,當我試圖創建KidReads
時,我遇到了編譯時問題。
scala> implicit val KidReads: Reads[Kid] = (
| (JsPath \ "friends").read[List[Friend]]
|)(Kid.apply _)
<console>:40: error: overloaded method value read with alternatives:
(t: List[Friend])play.api.libs.json.Reads[List[Friend]] <and>
(implicit r: play.api.libs.json.Reads[List[Friend]])play.api.libs.json.Reads[List[Friend]]
cannot be applied to (List[Friend] => Kid)
(JsPath \ "friends").read[List[Friend]]
^
如何解決此錯誤?
謝謝,特拉維斯,你是對的。我看到一種新類型,忘記檢查你的有用答案。我的錯。 –