scala spray.json 如何获取 Json 对象
scala spray.json how to get Json Object
如果我尝试 Http Get Response {"ReturnValue":""}
,
此代码出错。
Caused by: spray.json.DeserializationException: Expected List as
JsArray, but got {"ReturnValue":""}
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol
import spray.http._
import spray.client.pipelining._
import scala.concurrent.duration._
import scala.concurrent.{ Await, Future }
import akka.actor.ActorSystem
import scala.concurrent.ExecutionContext.Implicits.global
class ApiHelper extends DefaultJsonProtocol {
case class Robot(name: String, color: Option[String], amountOfArms: Int)
implicit val RobotFormat = jsonFormat3(Robot)
def CallAPI(httpMethod: String, subURL: String): String = {
val apiLocation = "~~~"
val timeout = 5.seconds
implicit val system = ActorSystem("robotClient")
return httpMethod match {
case "GET" =>
val pipeline: HttpRequest => Future[List[Robot]] = sendReceive ~> unmarshal[List[Robot]]
val f: Future[List[Robot]] = pipeline(Get(s"$apiLocation"+subURL))
val robots = Await.result(f, timeout)
println(s"Got the list of robots: $robots")
return "hello"
}
}
}
Caused by: spray.json.DeserializationException: Expected List as JsArray, but got {"ReturnValue":""} at
spray.json.package$.deserializationError(package.scala:23) at
spray.json.CollectionFormats$$anon.read(CollectionFormats.scala:29)
at
spray.json.CollectionFormats$$anon.read(CollectionFormats.scala:25)
at
spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller.applyOrElse(SprayJsonSupport.scala:37)
at
spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller.applyOrElse(SprayJsonSupport.scala:34)
at
scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at
spray.httpx.unmarshalling.Unmarshaller$$anon$$anonfun$unmarshal.apply(Unmarshaller.scala:29)
at
spray.httpx.unmarshalling.SimpleUnmarshaller.protect(SimpleUnmarshaller.scala:40)
at
spray.httpx.unmarshalling.Unmarshaller$$anon.unmarshal(Unmarshaller.scala:29)
at
spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:29)
at
spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:23)
at
spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:35)
at
spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:34)
at
spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:30)
at
spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:29)
at
spray.httpx.unmarshalling.package$PimpedHttpResponse.as(package.scala:51)
at
spray.httpx.ResponseTransformation$$anonfun$unmarshal.apply(ResponseTransformation.scala:33)
... 13 more
有什么方法可以得到Json对象吗?
您可以提供并使用您自己的 unmarshal 实现,它将构造 JsValue 而不是 List[Robot]。 JsValue 将代表有效响应(机器人列表)或任意 json 响应(或可能更多自定义对象类型)。
def unmarshal: HttpResponse ⇒ JsValue =
response ⇒
if (response.status.isSuccess)
response.as[List[Robot]] match {
case Right(value) ⇒ value.toJson
case Left(error: MalformedContent) ⇒
response.as[JsObject] match {
case Right(value) ⇒ value.toJson
case Left(error) => throw new PipelineException(error.toString)
}
case Left(error) ⇒ throw new PipelineException(error.toString)
}
else throw new UnsuccessfulResponseException(response.status)
在未来(调用管道)returns JsValue 之后,您可以尝试以受控方式(例如在 Try 块内)将其再次转换回 List[Robot] 并在失败的情况下处理它作为自定义 json 回复。
如果我尝试 Http Get Response {"ReturnValue":""}
,
此代码出错。
Caused by: spray.json.DeserializationException: Expected List as JsArray, but got {"ReturnValue":""}
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol
import spray.http._
import spray.client.pipelining._
import scala.concurrent.duration._
import scala.concurrent.{ Await, Future }
import akka.actor.ActorSystem
import scala.concurrent.ExecutionContext.Implicits.global
class ApiHelper extends DefaultJsonProtocol {
case class Robot(name: String, color: Option[String], amountOfArms: Int)
implicit val RobotFormat = jsonFormat3(Robot)
def CallAPI(httpMethod: String, subURL: String): String = {
val apiLocation = "~~~"
val timeout = 5.seconds
implicit val system = ActorSystem("robotClient")
return httpMethod match {
case "GET" =>
val pipeline: HttpRequest => Future[List[Robot]] = sendReceive ~> unmarshal[List[Robot]]
val f: Future[List[Robot]] = pipeline(Get(s"$apiLocation"+subURL))
val robots = Await.result(f, timeout)
println(s"Got the list of robots: $robots")
return "hello"
}
}
}
Caused by: spray.json.DeserializationException: Expected List as JsArray, but got {"ReturnValue":""} at
spray.json.package$.deserializationError(package.scala:23) at spray.json.CollectionFormats$$anon.read(CollectionFormats.scala:29) at spray.json.CollectionFormats$$anon.read(CollectionFormats.scala:25) at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller.applyOrElse(SprayJsonSupport.scala:37) at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller.applyOrElse(SprayJsonSupport.scala:34) at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) at spray.httpx.unmarshalling.Unmarshaller$$anon$$anonfun$unmarshal.apply(Unmarshaller.scala:29) at spray.httpx.unmarshalling.SimpleUnmarshaller.protect(SimpleUnmarshaller.scala:40) at spray.httpx.unmarshalling.Unmarshaller$$anon.unmarshal(Unmarshaller.scala:29) at spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:29) at spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:23) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:35) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:34) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:30) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon.apply(UnmarshallerLifting.scala:29) at spray.httpx.unmarshalling.package$PimpedHttpResponse.as(package.scala:51) at spray.httpx.ResponseTransformation$$anonfun$unmarshal.apply(ResponseTransformation.scala:33) ... 13 more
有什么方法可以得到Json对象吗?
您可以提供并使用您自己的 unmarshal 实现,它将构造 JsValue 而不是 List[Robot]。 JsValue 将代表有效响应(机器人列表)或任意 json 响应(或可能更多自定义对象类型)。
def unmarshal: HttpResponse ⇒ JsValue =
response ⇒
if (response.status.isSuccess)
response.as[List[Robot]] match {
case Right(value) ⇒ value.toJson
case Left(error: MalformedContent) ⇒
response.as[JsObject] match {
case Right(value) ⇒ value.toJson
case Left(error) => throw new PipelineException(error.toString)
}
case Left(error) ⇒ throw new PipelineException(error.toString)
}
else throw new UnsuccessfulResponseException(response.status)
在未来(调用管道)returns JsValue 之后,您可以尝试以受控方式(例如在 Try 块内)将其再次转换回 List[Robot] 并在失败的情况下处理它作为自定义 json 回复。