2017-12-12 1 views
0

Wenn ich versuche HTTP Antwort bekommen {"ReturnValue":""}, dieser Code Fehler machen.scala spray.json wie bekomme ich JSON Object

Verursacht durch: spray.json.DeserializationException: Erwartete Liste als JsArray, bekam aber { "Return": ""}

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" 
    } 
    } 
} 

Verursacht durch: spray.json.DeserializationException : Liste als JsArray erwartet, aber bekam { "Return": ""} bei

spray.json.package $ .deserializationError (package.scala: 23) bei spray.json.CollectionFormats $$ ano n $ 1.read (CollectionFormats.scala: 29) bei spray.json.CollectionFormats $$ $ Anon 1.read (CollectionFormats.scala: 25) bei spray.httpx.SprayJsonSupport $$ $ anonfun sprayJsonUnmarshaller $ 1.applyOrElse (SprayJsonSupport .scala: 37) bei spray.httpx.SprayJsonSupport $$ $ anonfun sprayJsonUnmarshaller $ 1.applyOrElse (SprayJsonSupport.scala: 34) bei scala.runtime.AbstractPartialFunction.apply (AbstractPartialFunction.scala: 36) bei Spray. httpx.unmarshalling.Unmarshaller $$ $ 1 Anon $$ $ anonfun unmarshal $ 1.Apply (Unmarshaller.scala: 29) bei spray.httpx.unmarshalling.SimpleUnmarshaller.protect (SimpleUnmarshaller.scala: 40) bei spray.httpx.unmarshalling.Unmarshaller $$ Anon $ 1.unmarshal (Unmarshaller.scala: 29) bei spray.httpx.unmarshalling.SimpleUnmarshaller.apply (SimpleUnmarshaller.scala: 29) bei spray.httpx.unmarshalling .SimpleUnmarshaller.apply (SimpleUnmarshaller.scala: 23) bei spray.httpx.unmarshalling.UnmarshallerLifting $$ Anon $ 3.Apply (UnmarshallerLifting.scala: 35) bei spray.httpx.unmarshalling.UnmarshallerLifting $$ Anon $ 3.Apply (UnmarshallerLifting.scala: 34) bei spray.httpx.unmarshalling.UnmarshallerLifting $$ $ Anon 2.Apply (UnmarshallerLifting.scala: 30) bei spray.httpx.unmarshalling.UnmarshallerLi fting $$ Anon $ 2.Apply (UnmarshallerLifting.scala: 29) bei spray.httpx.unmarshalling.package $ PimpedHttpResponse.as (package.scala: 51) bei spray.httpx.ResponseTransformation $$ anonfun $ Abstellungs $ 1. gelten (ResponseTransformation.scala: 33) ... 13 weitere

gibt es eine Möglichkeit JSON-Objekt zu bekommen?

Antwort

0

Sie können eine eigene Implementierung von unmarshal bereitstellen und verwenden, die JsValue anstelle von List [Robot] erstellen würde. JsValue würde entweder eine gültige Antwort (Liste von Robotern) oder eine beliebige json-Antwort (oder wahrscheinlich mehr benutzerdefinierte Objekttypen) darstellen.

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) 

Nachdem die Zukunft (Pipeline nennen) gibt JsValue Sie können versuchen, es wieder zu konvertieren zurück zur Liste [Robot] in einer kontrollierten Art und Weise (zB innerhalb eines Try-Block) und im Falle des Scheiterns als ein Griff benutzerdefinierte JSON-Antwort.