首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >scala spray.json如何获取Json对象

scala spray.json如何获取Json对象
EN

Stack Overflow用户
提问于 2017-12-12 04:15:06
回答 1查看 1.2K关注 0票数 0

如果我尝试Http响应{"ReturnValue":""},这个代码会出错。

由: spray.json.DeserializationException:预期列表为JsArray引起,但得到{"ReturnValue":""}

代码语言:javascript
运行
复制
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"
    }
  }
}

由: spray.json.DeserializationException:预期列表为JsArray引起,但得到{"ReturnValue":""} at spray.json.package$.deserializationError(package.scala:23) at spray.json.CollectionFormats$$anon$1.read(CollectionFormats.scala:29) at spray.json.CollectionFormats$$anon$1.read(CollectionFormats.scala:25) at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:37) at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:34) at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) at spray.httpx.unmarshalling.Unmarshaller$$anon$1$$anonfun$unmarshal$1.apply(Unmarshaller.斯卡拉:29)在spray.httpx.unmarshalling.SimpleUnmarshaller.protect(SimpleUnmarshaller.scala:40) at spray.httpx.unmarshalling.Unmarshaller$$anon$1.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$3.apply(UnmarshallerLifting.scala:35) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon$3.apply(UnmarshallerLifting.斯卡拉:34)在spray.httpx.unmarshalling.UnmarshallerLifting$$anon$2.apply(UnmarshallerLifting.scala:30) at spray.httpx.unmarshalling.UnmarshallerLifting$$anon$2.apply(UnmarshallerLifting.scala:29) at spray.httpx.unmarshalling.package$PimpedHttpResponse.as(package.scala:51) at spray.httpx.ResponseTransformation$$anonfun$unmarshal$1.apply(ResponseTransformation.scala:33) . 13

有办法得到Json对象吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-13 01:48:05

您可以提供和使用您自己的反封送处理实现,它将构造JsValue而不是ListRobot。JsValue将表示有效响应(机器人列表)或任意json响应(或者更多自定义对象类型)。

代码语言:javascript
运行
复制
  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)

在将来(对管道的调用)返回JsValue之后,您可以尝试以受控的方式(例如在try块中)将其转换回ListRobot,并在失败时将其作为自定义的json响应处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47765257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档