Akka 在spray json中解压嵌套json
使用spray json(就像我使用spray客户端一样)为了从google maps API获取纬度和经度对象,我需要设置整个响应结构:Akka 在spray json中解压嵌套json,akka,spray,spray-json,Akka,Spray,Spray Json,使用spray json(就像我使用spray客户端一样)为了从google maps API获取纬度和经度对象,我需要设置整个响应结构: case class AddrComponent(long_name: String, short_name: String, types: List[String]) case class Location(lat: Double, lng: Double) case class ViewPort(northeast: Location, southwes
case class AddrComponent(long_name: String, short_name: String, types: List[String])
case class Location(lat: Double, lng: Double)
case class ViewPort(northeast: Location, southwest: Location)
case class Geometry(location: Location, location_type: String, viewport: ViewPort)
case class EachResult(address_components: List[AddrComponent],
formatted_address: String,
geometry: Geometry,
types: List[String])
case class GoogleApiResult[T](status: String, results: List[T])
object AddressProtocol extends DefaultJsonProtocol {
implicit val addrFormat = jsonFormat3(AddrComponent)
implicit val locFormat = jsonFormat2(Location)
implicit val viewPortFormat = jsonFormat2(ViewPort)
implicit val geomFormat = jsonFormat3(Geometry)
implicit val eachResFormat = jsonFormat4(EachResult)
implicit def GoogleApiFormat[T: JsonFormat] = jsonFormat2(GoogleApiResult.apply[T])
}
import AddressProtocol._
有没有办法从响应中的json获取位置
,避免所有这些口香糖
spray客户端代码:
implicit val system = ActorSystem("test-system")
import system.dispatcher
private val pipeline = sendReceive ~> unmarshal[GoogleApiResult[EachResult]]
def getPostcode(postcode: String): Point = {
val url = s"http://maps.googleapis.com/maps/api/geocode/json?address=$postcode,+UK&sensor=true"
val future = pipeline(Get(url))
val result = Await.result(future, 10 seconds)
result.results.size match {
case 0 => throw new PostcodeNotFoundException(postcode)
case x if x > 1 => throw new MultipleResultsException(postcode)
case _ => {
val location = result.results(0).geometry.location
new Point(location.lng, location.lat)
}
}
}
或者,我如何将jackson与spray客户端一起使用?按照jrudolph对json镜头的建议,我也做了不少修改,但最终还是成功了。我发现这很难(作为一个新手),而且我确信这个解决方案远不是最优雅的——尽管如此,我认为这可能会帮助人们或激励其他人进行改进 给定JSON:
{
"status": 200,
"code": 0,
"message": "",
"payload": {
"statuses": {
"emailConfirmation": "PENDING",
"phoneConfirmation": "DONE",
}
}
}
仅用于解组状态的大小写类:
case class UserStatus(emailConfirmation: String, phoneConfirmation: String)
可以这样做来解组响应:
import scala.concurrent.Future
import spray.http.HttpResponse
import spray.httpx.unmarshalling.{FromResponseUnmarshaller, MalformedContent}
import spray.json.DefaultJsonProtocol
import spray.json.lenses.JsonLenses._
import spray.client.pipelining._
object UserStatusJsonProtocol extends DefaultJsonProtocol {
implicit val userStatusUnmarshaller = new FromResponseUnmarshaller[UserStatus] {
implicit val userStatusJsonFormat = jsonFormat2(UserStatus)
def apply(response: HttpResponse) = try {
Right(response.entity.asString.extract[UserStatus]('payload / 'statuses))
} catch { case x: Throwable =>
Left(MalformedContent("Could not unmarshal user status.", x))
}
}
}
import UserStatusJsonProtocol._
def userStatus(userId: String): Future[UserStatus] = {
val pipeline = sendReceive ~> unmarshal[UserStatus]
pipeline(Get(s"/api/user/${userId}/status"))
}
你可以试着从JSON ast中很容易地提取数据。太棒了,做了一些修改,但现在看起来很棒。这将被合并到spray json中吗?应该是!!!将其与spray json合并是我们的计划,但我们目前没有能力进行移动。