Json 객체를 디스크에서 읽고 쓸 수 있어야합니다.디스크의 스프레이 json 객체에 쓰거나 읽는 방법은 무엇입니까?
나는 Java에서 10 분 정도 걸릴 것이라고 인정했다.
스칼라는 좀 더 도전적입니다. 주된 이유는 그물에 대한 충분한 정보가 아니라고 생각합니다.
어쨌든, 이것은 내가 지금까지 무엇을했는지 있습니다 :
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'가 무엇입니까? JSON을'write' 또는'read'하려고합니까? –
미안 Soumya, 의미는 : foo.toJson. 어쨌든,이 일을 : x.toJson.toString() – bashan