我有以下简单的代码
val fileLine = s"""{"AP_details": [{"MAC_Address": "00:11:74:00:00:00"},{"MAC_Address": "00:11:74:00:00:00"},{"MAC_Address": "00:11:74:00:00:00"}]}
"""
val jsonAst = fileLine.parseJson
import APDetailsJsonProtocol._
jsonAst.convertTo[APDetails]
转换在另一个文件中定义为
包裹测试 import spray.json._ import DefaultJsonProtocol._
case class APDetails(val mAPRadioOperatingParams:List[APDetail])
case class APDetail(val mac_id:String)
object APDetailsJsonProtocol extends DefaultJsonProtocol {
implicit val APDetailFormat = jsonFormat1(APDetail)
implicit object APDetailsJsonFormat extends RootJsonFormat[APDetails] {
def write(c: APDetails) = ???
def read(value: JsValue) = value.asJsObject.getFields("AP_Details") match
{
case Seq(jsv ) => new APDetails(jsv.convertTo[List[APDetail]])
}
}
}
但是我得到以下异常
Exception in thread "main" scala.MatchError: Vector() (of class scala.collection.immutable.Vector)
at test.APDetailsJsonProtocol$APDetailsJsonFormat$.read(APDetails.scala:13)
at test.APDetailsJsonProtocol$APDetailsJsonFormat$.read(APDetails.scala:11)
at spray.json.JsValue.convertTo(JsValue.scala:31)
at test.bootStrap.<init>(bootStrap.scala:25)
at test.TestApp$.delayedEndpoint$test$TestApp$1(TestApp.scala:10)
at test.TestApp$delayedInit$body.apply(TestApp.scala:4)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at test.TestApp$.main(TestApp.scala:4)
at test.TestApp.main(TestApp.scala)
我做错了什么? 最好的祝福, 维沙尔