我有以下案例类:
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())))
)
)
问题是什么?我不明白为什么这会失败!