我正在使用spray-json
这就是我正在做的事情
import java.io.File
import spray.json._
import scala.io.Source
case class WorkConfig(configName: String,
logSource: String,
logType: String,
afterProcessingFileAction: String,
recursiveFind: Boolean = false,
processZipFiles: Boolean = false) {
}
object WorkConfigParser {
object WorkOrderItemJsonProtocol extends DefaultJsonProtocol {
implicit val workOrderItemFormat = jsonFormat6(WorkConfig.apply)
}
def get(workOrderJson: File): List[WorkConfig] = {
import WorkOrderItemJsonProtocol._
val json: JsValue = Source.fromFile(workOrderJson).mkString.toJson
json.convertTo[List[WorkConfig]]
}
def main(args: Array[String]) {
val items: List[WorkConfig] = WorkConfigParser.get(new File("resources/workConfigSample.json"))
items.foreach((x: WorkConfig) => println(x.afterProcessingFileAction))
}
}
当我运行此代码时,我看到
Exception in thread "main" spray.json.DeserializationException: Expected List as JsArray, but got "[\n {\n \"configName\": \"bluecoat\",\n \"logSource\": \"/root/fw1/logs/bc\",\n \"logType\": \"bluecoat\",\n \"recursiveFind\": true,\n \"processZipFiles\": false,\n \"afterProcessingFileAction\": \"delete\"\n },\n {\n \"configName\": \"mcAfee\",\n \"logSource\": \"/root/fw1/logs/mcafee\",\n \"logType\": \"mcafee\",\n \"recursiveFind\": true,\n \"processZipFiles\": true,\n \"afterProcessingFileAction\": \"delete\"\n }\n]\n\n\n"
at spray.json.package$.deserializationError(package.scala:23)
at spray.json.CollectionFormats$$anon$1.read(CollectionFormats.scala:29)
at spray.json.CollectionFormats$$anon$1.read(CollectionFormats.scala:25)
at spray.json.JsValue.convertTo(JsValue.scala:31)
at com.logprocessor.inputs.WorkConfigParser$.get(WorkConfig.scala:27)
at com.logprocessor.inputs.WorkConfigParser$.main(WorkConfig.scala:31)
at com.logprocessor.inputs.WorkConfigParser.main(WorkConfig.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
这里出了什么问题?
答案 0 :(得分:3)
您正在使用toJson而不是使用parseJson。 - 将json String转换为JsValue应该使用parseJson。
@ val w = WorkConfig("a","b","c","d")
w: WorkConfig = WorkConfig("a", "b", "c", "d", false, false)
@ val l = List(w,w)
l: List[WorkConfig] = List(WorkConfig("a", "b", "c", "d", false, false),
WorkConfig("a", "b", "c", "d", false, false))
@ l.toJson.toString
res48: String = """
[{"configName":"a","logType":"c","recursiveFind":false,
"processZipFiles":false,"afterProcessingFileAction":"d","logSource":"b
{"configName":"a","logType":"c","recursiveFind":false,
"processZipFiles":false,"afterProcessingFileAction":"d","logSource":"b"}]
"""
@ res48.parseJson
res49: JsValue = [{"configName":"a","logType":"c","recursiveFind":false,
"processZipFiles":false,"afterProcessingFileAction":"d","logSource":"b
{"configName":"a","logType":"c","recursiveFind":false,
"processZipFiles":false,"afterProcessingFileAction":"d","logSource":"b"}]
@ res49.convertTo[List[WorkConfig]]
res50: List[WorkConfig] = List(WorkConfig("a", "b", "c", "d", false,
false), WorkConfig("a", "b", "c", "d", false, false))