播放 JSON 读取序列时读取错误

Play JSON Read Error When Reading a Sequence

我有以下情况class:

case class VirtualAssetConfigParam(
  id: Long,
  pMin: Double,
  pMax: Double,
  dispatchPriority: Int
)

object VirtualAssetConfigParam {

  implicit val virtualAssetConfigParamReads: Reads[VirtualAssetConfigParam] = (
    (JsPath \ "id").read[Long] and
      (JsPath \ "power_min").read[Double] and
      (JsPath \ "power_max").read[Double] and
      (JsPath \ "dispatch_priority").read[Int]
    )(VirtualAssetConfigParam.apply _)
}

这是我从数据库中得到的JSON:

[{"id":"1","power_min":"200","power_max":"400","dispatch_priority":"1"},{"id":"2","power_min":"200","power_max":"400","dispatch_priority":"2"},{"id":"3","power_min":"-700","power_max":"0","dispatch_priority":"3"}]

当我尝试将其验证为(其中 virtualAssetConfigParam 是我从数据库中获取的字符串)时:

Json.parse(virtualAssetConfigParam).validate[List[VirtualAssetConfigParam]]

我得到以下验证结果:

JsError(
    List(
        ((0)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((0)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((0)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((0)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray()))), 
        ((1)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((1)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((1)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((1)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray()))), 
        ((2)/dispatchPriority,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((2)/pMin,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((2)/pMax,List(ValidationError(List(error.path.missing),WrappedArray()))), 
        ((2)/id,List(ValidationError(List(error.expected.jsnumber),WrappedArray())))
    )
)

问题是什么?我不明白为什么这会失败!

如果您查看错误,您会注意到它正在尝试查找路径 - dispatchPrioritypMinpMaxid,这与read 中您想要的路径。这表明此处未使用您在代码中显示的读取实现。

这让我认为您可能没有将隐式读取导入代码范围。

case class VirtualAssetConfigParam(
  id: Long,
  pMin: Double,
  pMax: Double,
  dispatchPriority: Int
)

object VirtualAssetConfigParam {

  implicit val virtualAssetConfigParamReads: Reads[VirtualAssetConfigParam] = (
    (JsPath \ "id").read[Long] and
    (JsPath \ "power_min").read[Double] and
    (JsPath \ "power_max").read[Double] and
    (JsPath \ "dispatch_priority").read[Int]
  )(VirtualAssetConfigParam.apply _)

}

object Demo extends App {

  // I think you are missing this import
  import VirtualAssetConfigParam.virtualAssetConfigParamReads

  val virtualAssetConfigParam = [{"id":"1","power_min":"200","power_max":"400","dispatch_priority":"1"},{"id":"2","power_min":"200","power_max":"400","dispatch_priority":"2"},{"id":"3","power_min":"-700","power_max":"0","dispatch_priority":"3"}]

 // It should work if you have that import
 val jsonValidate = Json.parse(virtualAssetConfigParam).validate[List[VirtualAssetConfigParam]]


}

JSON 中的数据类型与案例 class 中的预期数据类型不匹配。所以案例 class 应该是:

  implicit val virtualAssetConfigParamReads: Reads[VirtualAssetConfigParam] = new Reads[VirtualAssetConfigParam] {
    override def reads(json: JsValue): JsResult[VirtualAssetConfigParam] = {
      JsSuccess(VirtualAssetConfigParam(
        (json \ "id").as[String].toLong,
        (json \ "power_min").as[String].toDouble,
        (json \ "power_max").as[String].toDouble,
        (json \ "dispatch_priority").as[String].toInt
      ))
    }
  }

当我现在调用 JSON 上的解析函数时,我是这样做的:

Try((Json.parse(json)).validate[Seq[VirtualAssetConfigParam]])

然后在尝试成功时弃牌!