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

时间:2016-10-04 08:50:44

标签: json scala playframework play-json

我有以下案例类:

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是我从数据库获取的String):

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())))
    )
)

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

2 个答案:

答案 0 :(得分:0)

如果您查看错误,您会发现它正在尝试查找路径 - 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]]


}

答案 1 :(得分:0)

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

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

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

import UIKit

let mString = "This is a String that contains something to search."
let stringToSearchUpperCase = "String"
let stringToSearchLowerCase = "string"

mString.contains(stringToSearchUpperCase) //true
mString.contains(stringToSearchLowerCase) //false
mString.lowercased().contains(stringToSearchUpperCase) //false
mString.lowercased().contains(stringToSearchLowerCase) //true

然后折叠尝试成功!