2

注意:查看问题底部的代码以查看答案

我将 DataNode 添加为自身的 List 属性并开始出现以下编译错误。是否有可能拥有它所在类的属性?

我收到编译错误:

ItemInquiry.scala:21:没有找到类型 List[models.DataNode] 的 Json 格式化程序。尝试为此类型实现隐式格式。[错误] (__ \ "dataNodes" ).format[List[DataNode]]

package models

import play.api.libs.json._
import play.api.libs.functional.syntax._


case class ItemSearchResult(criteria: String, site: String, searchTime: Int, exact: Boolean, searchResults: List[Item])

case class Item(name: String, upc: String, description: String, dataNodes: List[DataNode])

case class DataNode(typeVal: String, title: String, subTitle: String, requiredRole: String, dataNodes: List[DataNode])


object ItemSearchResult {

  implicit val dataNode = (
    (__ \ "type"        ).format[String] and
    (__ \ "title"       ).format[String] and
    (__ \ "requiredRole").format[String] and
    (__ \ "subTitle"    ).format[String] and
    (__ \ "dataNodes"   ).format[List[DataNode]]
   )(DataNode.apply _, unlift(DataNode.unapply _))

  implicit val item = ((__ \ "name").format[String] and
    (__ \ "upc").format[String] and
    (__ \ "description").format[String] and
    (__ \ "dataNodes").format[List[DataNode]])(Item.apply _, unlift(Item.unapply _))

  implicit val itemSearchResult = (
    (__ \ "criteria").format[String] and
    (__ \ "site").format[String] and
    (__ \ "searchTime").format[Int] and
    (__ \ "exact").format[Boolean] and
    (__ \ "searchResults").format[List[Item]])(ItemSearchResult.apply _, unlift(ItemSearchResult.unapply _))
}

更新了修复我的问题的确切代码:

package models

import play.api.libs.json._
import play.api.libs.functional.syntax._


case class ItemSearchResult(criteria: String, site: String, searchTime: Int, exact: Boolean, searchResults: List[Item])

case class Item(name: String, upc: String, description: String, dataNodes: List[DataNode])

case class DataNode(typeVal: String, title: String, subTitle: String, requiredRole: String, dataNodes: List[DataNode])

object ItemSearchResult {

  implicit val itemSearchResultFormat : Writes[ItemSearchResult] =(
      (__ \'criteria).write[String] and
      (__ \'site).write[String] and
      (__ \'searchTime).write[Int] and
      (__ \'exact).write[Boolean] and
      (__ \'searchResults).lazyWrite(Writes.traversableWrites[Item](itemFormat))
    )(unlift(ItemSearchResult.unapply))

  implicit val itemFormat : Writes[Item] =(
      (__ \'name).write[String] and
      (__ \'upc).write[String] and
      (__ \'description).write[String] and
      (__ \'dataNodes).lazyWrite(Writes.traversableWrites[DataNode](dataNodeFormat))
    )(unlift(Item.unapply))

  implicit val dataNodeFormat : Writes[DataNode] =(
      (__ \'type).write[String] and
      (__ \'title).write[String] and
      (__ \'subTitle).write[String] and
      (__ \'requiredRole).write[String] and
      (__ \'dataNodes).lazyWrite(Writes.traversableWrites[DataNode](dataNodeFormat))
    )(unlift(DataNode.unapply))
}
4

1 回答 1

2

绝对可以,这是我的代码可以正常工作:

在模型中:

case class Category(

id : Int,
name: String,
subcutegory : List[Category]
)

object Category{

  implicit val categoryFormat : Writes[Category] =(
   (__ \'id).write[Int] and
       (__ \'name).write[String]and
          (__ \'subcategory).lazyWrite(Writes.traversableWrites[Category](categoryFormat))
    )(unlift(Category.unapply))
}

所以这里的关键时刻是如何为 field 实现 json 格式化程序subcategory

然后你就可以毫无问题地渲染你的jsonJson.toJson(....)

于 2013-08-30T23:27:26.697 回答