我尝试使用本文ScalaJsonCombinators
来实施验证基本上我想获得值,如果存在,如果不返回 null
val nextPage: JsResult[JsValue] = (ch.\("paging").\("next")).validate[JsValue]
val nextUrl: String = nextPage match {
case s: JsSuccess[String] => s.get
case e: JsError => null
}
第一个是警告
Warning:(99, 19) non-variable type argument String in type pattern play.api.libs.json.JsSuccess[String] is unchecked since it is eliminated by erasure
case s: JsSuccess[String] => s.get
^
第二个是错误,因为字符串是带特殊的URI 字符我得到一个方案错误
val nextPage:JsResult [JsValue] =(ch。(" paging")。(" next"))。验证[JsValue]
val nextUrl: String = nextPage match {
case s: JsSuccess[String] => s.toString
case e: JsError => null
}
java.lang.IllegalArgumentException: Illegal character in scheme name at index 9: JsSuccess(https://graph.facebook.com/v2.2/396697410351933/feed?limit=200&access_token=623501864400497%7CGumAg3k6Eu
如何在不使用序列化的情况下更正验证此元素的方法。
感谢,
miki
答案 0 :(得分:1)
您的身份验证不正确。在validate[JsValue]
上调用JsValue
毫无意义,因为始终会导致JsSuccess[JsValue]
。另一个问题是,您是否尝试将JsSuccess[String]
与JsSuccess[JsValue]
进行模式匹配,由于多种原因您无法做到这一点。一,类型参数在运行时被擦除,如编译器警告所示。另外两个,JsSuccess[String]
不能是JsSuccess[JsValue]
,这些类型并不相关。
您真正需要的是validate[String]
。
除非您提供相关的JSON,否则可能无法调试。
null
丢弃),因此您可以使用asOpt
。我在这里说如果有可能没有next
,那么nextUrl
应该是Option[String]
而不是String
,你永远不应该使用Scala中的null
。
val nextUrl: Option[String] = (ch \ "paging" \ "next").asOpt[String]
如果由于某种原因必须使用null
:
val nextUrl: String = (ch \ "paging" \ "next").asOpt[String].getOrElse(null)