使用spray json将json转换为scala对象数组

时间:2017-11-28 15:16:29

标签: json scala spray-json

我对喷json不太熟悉,但我必须将下面的json转换为Array [myTest]

下面是代码,但它不起作用。它会引发以下错误:如何修复它们?

Error:(19, 54) Cannot find JsonReader or JsonFormat type class for Array[A$A61.this.myTest]
lazy val converted= trainingDataRef.toJson.convertTo[Array[myTest]]
                                                    ^
Error:(19, 54) not enough arguments for method convertTo: (implicit evidence$1: spray.json.JsonReader[Array[A$A61.this.myTest]])Array[A$A61.this.myTest].
Unspecified value parameter evidence$1.
lazy val converted= trainingDataRef.toJson.convertTo[Array[myTest]]
                                                    ^
Error:(10, 61) could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[Map[String,Any]]
  implicit val format: RootJsonFormat[myTest] = jsonFormat3(myTest.apply)


                                          ^


   Code:                           ^
import spray.json.DefaultJsonProtocol._
import spray.json._

case class myTest (
                        id: String,
                        classDetails: Map[String, Any],
                        school: Map[String, Any])
object myTest {
  implicit val format: RootJsonFormat[myTest] = jsonFormat3(myTest.apply)
}


val trainingDataRef = """[{"id":"my-id","classDetails":{"sec":"2","teacher":"John"},"school":{"name":"newschool"}}]"""

println(trainingDataRef.getClass)


val converted= trainingDataRef.toJson.convertTo[Array[myTest]]
println(converted)

1 个答案:

答案 0 :(得分:0)

spray-json有很好的文档,请看看那里。基本上,您必须定义案例类并为它们实现JsonFormat:

import spray.json.DefaultJsonProtocol._
import spray.json._

case class ClassDetails(sec: String, teacher: String)
object ClassDetails {
  implicit val format: RootJsonFormat[ClassDetails] = jsonFormat2(ClassDetails.apply)
}

case class School(name: String)
object School {
  implicit val format: RootJsonFormat[School] = jsonFormat1(School.apply)
}

case class ClassInfo
(
  id: String,
  classDetails: ClassDetails,
  school: School
)

object ClassInfo {
  implicit object ClassInfoFormat extends RootJsonFormat[ClassInfo] {
    def write(c: ClassInfo): JsValue = JsObject(
      "id" -> JsString(c.id),
      "classDetails" -> c.classDetails.toJson,
      "school" -> c.school.toJson
    )
    def read(value: JsValue): ClassInfo = {
      value.asJsObject.getFields("id", "classDetails", "school") match {
        case Seq(JsString(name), details, school) =>
          new ClassInfo(name, details.convertTo[ClassDetails], school.convertTo[School])
        case _ => throw new DeserializationException("ClassInfo expected")
      }
    }
  }
}

val json = """[{"id":"my-id","classDetails":{"sec":"2","teacher":"John"},"school":{"name":"newschool"}}]"""
// JSON string to case classes
val classInfos = json.parseJson.convertTo[Seq[ClassInfo]]
classInfos.zipWithIndex.foreach { case (c, idx) =>
  println(s"$idx => $c")
}
println
// Seq[ClassInfo] to JSON
println(s"$classInfos: ")
println(classInfos.toJson.prettyPrint)