gpt4 book ai didi

json - Spray-Json:如何解析Json阵列?

转载 作者:行者123 更新时间:2023-12-04 04:59:29 28 4
gpt4 key购买 nike

我是Spray-Json API的新手,并且试图解析Docker REST API的Json响应。

There是使用Spray-Json解析此Google Map Json响应的一个干净示例:

{
"results" : [
{
"elevation" : 8815.7158203125,
"location" : {
"lat" : 27.988056,
"lng" : 86.92527800000001
},
"resolution" : 152.7032318115234
}
],
"status" : "OK"
}

在上面的示例中,最外层是 Object。但是,我需要直接解析一个Json响应,其最外层是一个由容器信息组成的 Array,如下所示:
[
{
"Id": "8dfafdbc3a40",
"Image": "base:latest",
"Command": "echo 1",
"Created": 1367854155,
"Status": "Exit 0",
"Ports":[{"PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp"}],
"SizeRw":12288,
"SizeRootFs":0
},
{ ... },
{ ... }
]

这是我从Google map 示例改编而成的代码:
package main

import ...

case class Container(id: String, image: String, command: String, created: Long, status: String, ports: List[Port], sizeRW: Long, sizeRootFs: Long)
case class Port(privatePort: Long, publicPort: Long, portType: String)
case class DockerApiResult[T](results: List[T])

object ContainerListJsonProtocol extends DefaultJsonProtocol {
implicit val portFormat = jsonFormat3(Port)
implicit val containerFormat = jsonFormat8(Container)
implicit def dockerApiResultFormat[T :JsonFormat] = jsonFormat1(DockerApiResult.apply[T])
}

object Main extends App {

implicit val system = ActorSystem("simple-spray-client")
import system.dispatcher // execution context for futures below
val log = Logging(system, getClass)

log.info("Requesting containers info...")

import ContainerListJsonProtocol._
import SprayJsonSupport._
val pipeline = sendReceive ~> unmarshal[DockerApiResult[Container]]

val responseFuture = pipeline {
Get("http://<ip-address>:4243/containers/json")
}

responseFuture onComplete {
case Success(DockerApiResult(Container(_,_,_,_,_,_,_,_) :: _)) =>
log.info("Id of the found image: {} ")
shutdown()

case Success(somethingUnexpected) =>
log.warning("The Docker API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
shutdown()

case Failure(error) =>
log.error(error, "Couldn't get containers information")
shutdown()
}

def shutdown(): Unit = {
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
}
}

下面是我得到的异常( Object expected):
spray.httpx.PipelineException: MalformedContent(Object expected,Some(spray.json.DeserializationException: Object expected))

我当然会错过一些显而易见的事情,但是 如何使用Spray-Json解析Json数组?

另外,是否有一种简单的方法可以执行此操作而不必处理自定义JsonFormat或RootJsonFormat?

最佳答案

通过执行unmarshal[DockerApiResult[Container]],您告诉Spray-json您希望格式为以下形式的json对象:

{ results: [...] }

因为 case class DockerApiResult[T](results: List[T])被定义为具有包含列表的单个结果字段的对象。

相反,您需要执行以下操作:
unmarshal[List[Container]]

然后直接对结果列表进行操作(或在通过spray-json对其进行解析后将其包装在DockerApiResult中)。

如果您想将spray-json直接解码到DockerApiResult中,则可以编写具有以下内容的JsonFormat:
implicit object DockerApiResultFormat extends RootJsonFormat[DockerApiResult] {
def read(value: JsValue) = DockerApiResult(value.convertTo[List[Container]])
def write(obj: DockerApiResult) = obj.results.toJson
}

关于json - Spray-Json:如何解析Json阵列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20523462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com