在 spray-json 中解组 json

unmarshalling json in spray-json

我正在尝试解组 json 字符串。但最终会出错。无法正确理解错误。

下面是代码:

import akka.actor.ActorSystem
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import spray.json._
import scala.concurrent.Future
import org.json4s.{Serialization, jackson}

object Test extends App{
  val json = """{
               |    "entries": [
               |        {
               |            ".tag": "folder",
               |            "name": "Camera Uploads",
               |            "path_lower": "/camera uploads",
               |            "path_display": "/Camera Uploads",
               |            "id": "id:up-mXElWkuAAAAAAAAAHsw"
               |        },
               |        {
               |            ".tag": "file",
               |            "name": "roboQuestions.mov",
               |            "path_lower": "/roboquestions.mov",
               |            "path_display": "/roboQuestions.mov",
               |            "id": "id:up-mXElWkuAAAAAAAAAUUg",
               |            "client_modified": "2016-08-23T16:24:53Z",
               |            "server_modified": "2016-08-23T18:13:34Z",
               |            "rev": "1a9d06ecb0f6",
               |            "size": 110339964
               |        }
               |    ],
               |    "cursor": "AAFA2NDm5UrAaaG6s2gwGbyh1Fq5Na6EE-UMpccSv2PrvAmfh-54645-6f4S2cYmnj0G0r1aMMy0KlP9VWllp3oWIaYtAKDQfj3zSCiWo_lB98MnRlEKDA0sRexYsTI68dlaDM_Yimiy",
               |    "has_more": false
               |}""".stripMargin.parseJson

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val jacksonSerialization: Serialization = jackson.Serialization
  import concurrent.ExecutionContext.Implicits.global

  final case class BoxFolder(id:String, `.tag`:Option[String], name:Option[String], path_lower:Option[String], path_display:Option[String], client_modified:Option[String], server_modified:Option[String], rev:Option[String], size:Option[Long])
  object BoxFolder extends DefaultJsonProtocol with SprayJsonSupport
  {
    implicit val folderFormat = jsonFormat9(BoxFolder.apply)
  }

  final case class BoxList(entries:List[BoxFolder], cursor:String, has_more:Boolean)
  object BoxList extends DefaultJsonProtocol with SprayJsonSupport
  {
    implicit val folderFormat = jsonFormat3(BoxList)
  }     
  val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]
  resFuture.map(println)
  system.terminate()
}

错误

 Error:(279, 53) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[spray.json.JsValue,Test.BoxList]
      val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]

    Error:(279, 53) not enough arguments for method to: (implicit um: akka.http.scaladsl.unmarshalling.Unmarshaller[spray.json.JsValue,Test.BoxList], implicit ec: scala.concurrent.ExecutionContext, implicit mat: akka.stream.Materializer)scala.concurrent.Future[Test.BoxList].
    Unspecified value parameters um, ec, mat.
      val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]

唯一缺少的是 类型 JsValue 的隐式解组器 到您的目标类型 BoxList:

  implicit val boxListUnmarshaller: Unmarshaller[JsValue, BoxList] =
    Unmarshaller.strict(jsValue => boxListFormat.read(jsValue))

原始代码中的整个工作示例:

import akka.actor.ActorSystem
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
import akka.stream.ActorMaterializer
import spray.json._

import scala.concurrent.Future
import org.json4s.{Serialization, jackson}

object Test extends App {
  val json =
    """{
      |    "entries": [
      |        {
      |            ".tag": "folder",
      |            "name": "Camera Uploads",
      |            "path_lower": "/camera uploads",
      |            "path_display": "/Camera Uploads",
      |            "id": "id:up-mXElWkuAAAAAAAAAHsw"
      |        },
      |        {
      |            ".tag": "file",
      |            "name": "roboQuestions.mov",
      |            "path_lower": "/roboquestions.mov",
      |            "path_display": "/roboQuestions.mov",
      |            "id": "id:up-mXElWkuAAAAAAAAAUUg",
      |            "client_modified": "2016-08-23T16:24:53Z",
      |            "server_modified": "2016-08-23T18:13:34Z",
      |            "rev": "1a9d06ecb0f6",
      |            "size": 110339964
      |        }
      |    ],
      |    "cursor": "AAFA2NDm5UrAaaG6s2gwGbyh1Fq5Na6EE-UMpccSv2PrvAmfh-54645-6f4S2cYmnj0G0r1aMMy0KlP9VWllp3oWIaYtAKDQfj3zSCiWo_lB98MnRlEKDA0sRexYsTI68dlaDM_Yimiy",
      |    "has_more": false
      |}""".stripMargin.parseJson

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val jacksonSerialization: Serialization = jackson.Serialization

  import concurrent.ExecutionContext.Implicits.global

  case class BoxFolder(id: String,
                       `.tag`: Option[String],
                       name: Option[String],
                       path_lower: Option[String],
                       path_display: Option[String],
                       client_modified: Option[String],
                       server_modified: Option[String],
                       rev: Option[String],
                       size: Option[Long])

  object BoxFolder extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val folderFormat = jsonFormat9(BoxFolder.apply)
  }

  case class BoxList(entries: List[BoxFolder], cursor: String, has_more: Boolean)

  object BoxListFormat extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val boxListFormat = jsonFormat3(BoxList)
  }

  import BoxListFormat._

  implicit val boxListUnmarshaller: Unmarshaller[JsValue, BoxList] =
    Unmarshaller.strict(jsValue => boxListFormat.read(jsValue))

  val resFuture: Future[BoxList] = Unmarshal(json).to[BoxList]
  resFuture.map(println)
  system.terminate()
}

希望对您有所帮助,先生!