【发布时间】:2014-11-29 19:20:23
【问题描述】:
我希望能够从磁盘读取/写入 Json 对象。
我承认,在 Java 中我需要大约 10 分钟。
Scala 更具挑战性。我认为主要原因是网上信息不够。
不管怎样,这就是我目前所做的:
package com.example
import java.io.{BufferedWriter, FileWriter}
import spray.json._
import spray.json.DefaultJsonProtocol
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
object Test {
object Foo extends DefaultJsonProtocol {
implicit val fooFormat = jsonFormat2(Foo.apply)
}
case class Foo(name: String, x: String) {
//def toJson:JsValue = JsObject( "name" -> JsString(name) )
}
def main(args: Array[String]) {
println("Hello, world!")
implicit val foo = new Foo("xxx", "jj")
println(foo.toJson)
val w = new BufferedWriter(new FileWriter("output.txt"))
w.write(x.toJson) // This doesn't work. I also tried: x.toJson.toString
}
}
【问题讨论】:
-
您的代码无法编译。最后一行的
x是什么?你想write或readJSON 吗? -
对不起 Soumya,意思是:foo.toJson。无论如何,这有效:x.toJson.toString()
标签: scala serialization spray-json