Scala 找不到Seq[models.Comment]类型的Json格式化程序。请尝试实现此类型的隐式格式

Scala 找不到Seq[models.Comment]类型的Json格式化程序。请尝试实现此类型的隐式格式,scala,playframework,playframework-2.0,Scala,Playframework,Playframework 2.0,您好scala和playFramework大师 我不明白为什么隐式Json.format[Comment]在我的代码中不起作用。从文档描述来看,它应该是格式[注释],但看起来不是这样 这是我的两个案例类及其同伴的代码 case class ServiceTask(id: Option[String], name: String, description: String,

您好scala和playFramework大师

我不明白为什么隐式
Json.format[Comment]
在我的代码中不起作用。从文档描述来看,它应该是
格式[注释]
,但看起来不是这样

这是我的两个案例类及其同伴的代码

case class ServiceTask(id: Option[String],
                       name: String,
                       description: String,
                       requiredInfo: String,
                       status: String,
                       approved: Boolean,
                       comments: Option[Seq[Comment]])
object ServiceTask {

  implicit val serviceTaskFormat: Format[ServiceTask] = (
    (JsPath \ "id").formatNullable[String] and
    (JsPath \ "name").format[String] and
    (JsPath \ "description").format[String] and
    (JsPath \ "requiredInfo").format[String] and
    (JsPath \ "status").format[String] and
    (JsPath \ "approved").format[Boolean] and
    (JsPath \ "comments").formatNullable[Seq[Comment]]
  )(ServiceTask.apply _, unlift(ServiceTask.unapply))

}


似乎您没有在
对象服务任务格式
中导入
注释格式
。试试这个:

object ServiceTask {
  import Comment._

  implicit val serviceTaskFormat: Format[ServiceTask] = (
    (JsPath \ "id").formatNullable[String] and
    (JsPath \ "name").format[String] and
    (JsPath \ "description").format[String] and
    (JsPath \ "requiredInfo").format[String] and
    (JsPath \ "status").format[String] and
    (JsPath \ "approved").format[Boolean] and
    (JsPath \ "comments").formatNullable[Seq[Comment]]
  )(ServiceTask.apply _, unlift(ServiceTask.unapply))
}

正确的。在
ServiceTask
my
Comment
中,隐式格式以前是不可见的。谢谢提醒您,当您的主要案例类中有多个内部案例类时,需要先定义内部案例类的json
格式
,为主要案例类定义json
格式
;(JsPath\“id”).formatNullable[Long]
object ServiceTask {
  import Comment._

  implicit val serviceTaskFormat: Format[ServiceTask] = (
    (JsPath \ "id").formatNullable[String] and
    (JsPath \ "name").format[String] and
    (JsPath \ "description").format[String] and
    (JsPath \ "requiredInfo").format[String] and
    (JsPath \ "status").format[String] and
    (JsPath \ "approved").format[Boolean] and
    (JsPath \ "comments").formatNullable[Seq[Comment]]
  )(ServiceTask.apply _, unlift(ServiceTask.unapply))
}