Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 仅使用Play Json进行验证_Scala_Play Json - Fatal编程技术网

Scala 仅使用Play Json进行验证

Scala 仅使用Play Json进行验证,scala,play-json,Scala,Play Json,我希望使用PlayJson只验证某些json的多个字段,而不是将其映射到自定义对象。我只关心验证标准的是或否答案。可以这样使用PlayJson吗?到目前为止,我有点像 val json = ..... val reads = (JsPath \ "foo").read[String](min(5)) and (JsPath \ "bar").read[String](max(10)) json.validate["I ONLY WANT TO VALIDATE NOT MAP"]

我希望使用PlayJson只验证某些json的多个字段,而不是将其映射到自定义对象。我只关心验证标准的是或否答案。可以这样使用PlayJson吗?到目前为止,我有点像

val json = .....

val reads = (JsPath \ "foo").read[String](min(5)) and
      (JsPath \ "bar").read[String](max(10))

json.validate["I ONLY WANT TO VALIDATE NOT MAP"](reads) match {
  case s: JsSuccess => true
  case e: JsError => false
}

谢谢Stack Overflow社区。

我们可以通过
Reads[(String,String)]
反序列化为元组,而不是通过
Reads[(String,String)]
进行反序列化

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

val reads = (
  (JsPath \ "foo").read[String](minLength[String](5)) and
  (JsPath \ "bar").read[String](minLength[String](10))
).tupled

val json = Json.parse(
  """
    |{
    |  "foo": "abcde",
    |  "bar": "woohoowoohoo",
    |  "zar": 42
    |}
    |""".stripMargin)

json.validate(reads).isSuccess
哪个输出

res0: Boolean = true
注意我们在创建读卡器时如何调用
tuple
方法,以及
issucess
如何从验证过程中获取布尔值


.validate.issucess
就这些。非常感谢您的回复。
.tuple
是我所缺少的。