Scala 在spray json中解组json

Scala 在spray json中解组json,scala,akka,json4s,spray-json,Scala,Akka,Json4s,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 sc

我正在尝试解组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()
}
希望有帮助,先生