我正在使用Play框架进行JSON解析,但遇到了以下空指针异常:
我的数据模型如下:
case class SearchLikeThisResult(total: Int, max_score: Double, hits: Seq[Hits])
case class Hits( index: String, typ: String, id: String, score: Double)
我的读者如下所示:
object SearchLikeThisHits {
import play.api.libs.functional.syntax._
implicit val searchLikeThisResult: Reads[SearchLikeThisResult] = (
(JsPath \ "total").read[Int] and
(JsPath \ "max_score").read[Double] and
(JsPath \ "hits").read[Seq[Hits]]
)(SearchLikeThisResult.apply _)
implicit val hitsReads: Reads[Hits] = (
(JsPath \ "_index").read[String] and
(JsPath \ "_type").read[String] and
(JsPath \ "_id").read[String] and
(JsPath \ "_score").read[Double]
)(Hits.apply _)
}
接下来是我的测试代码:
import play.api.libs.json.{JsValue, Json}
object Test extends App{
val str = """{"total": 53, "max_score": 3.2948244, "hits": [
{
"_index": "hovno",
"_type": "BYT",
"_id": "3413569628",
"_score": 3.2948244
},
{
"_index": "hovno22",
"_type": "BYT",
"_id": "3413569628",
"_score": 3.2948244
}
]
}"""
import SearchLikeThisHits.searchLikeThisResult
val json = Json.parse(str)
val r = json.as[SearchLikeThisResult]
}
这会导致以下空指针异常:
Exception in thread "main" java.lang.NullPointerException
at play.api.libs.json.Json$.fromJson(Json.scala:115)
at play.api.libs.json.DefaultReads$$anon$2$$anonfun$reads$6.apply(Reads.scala:448)
at play.api.libs.json.DefaultReads$$anon$2$$anonfun$reads$6.apply(Reads.scala:447)
at scala.collection.LinearSeqOptimized$class.foldLeft(LinearSeqOptimized.scala:111)
at scala.collection.immutable.List.foldLeft(List.scala:84)
at scala.collection.generic.TraversableForwarder$class.foldLeft(TraversableForwarder.scala:41)
at scala.collection.mutable.ListBuffer.foldLeft(ListBuffer.scala:45)
at play.api.libs.json.DefaultReads$$anon$2.reads(Reads.scala:447)
at play.api.libs.json.PathReads$$anonfun$at$1$$anonfun$apply$2.apply(JsConstraints.scala:36)
at play.api.libs.json.PathReads$$anonfun$at$1$$anonfun$apply$2.apply(JsConstraints.scala:36)
at play.api.libs.json.JsResult$class.flatMap(JsResult.scala:103)
at play.api.libs.json.JsSuccess.flatMap(JsResult.scala:9)
at play.api.libs.json.PathReads$$anonfun$at$1.apply(JsConstraints.scala:36)
at play.api.libs.json.PathReads$$anonfun$at$1.apply(JsConstraints.scala:36)
at play.api.libs.json.Reads$$anon$8.reads(Reads.scala:101)
at play.api.libs.json.Reads$$anon$3$$anon$4.reads(Reads.scala:81)
at play.api.libs.json.Reads$$anonfun$map$1.apply(Reads.scala:28)
at play.api.libs.json.Reads$$anonfun$map$1.apply(Reads.scala:28)
at play.api.libs.json.Reads$$anon$8.reads(Reads.scala:101)
at play.api.libs.json.JsValue$class.as(JsValue.scala:65)
at play.api.libs.json.JsObject.as(JsValue.scala:166)
at models.data.Test$delayedInit$body.apply(Test.scala:33)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at models.data.Test$.main(Test.scala:9)
at models.data.Test.main(Test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
如果我删除一个json数组那就一切正常了。我尝试了来自playframework的json支持网页上的类似示例,它们可以正确运行。我无法发现问题所在。根据我的调试猜测,可能存在隐式转换的问题,但是不知道如何进一步帮助自己。我尝试了编译器选项“-Xprint:typer”,但没有发现任何可疑的东西。任何提示将不胜感激。