Ich bin ein kompletter Neuling in Scala, alles was ich hier tun möchte, ist eine JSON-Datei einfach mit scala geparst und drucken Sie die gleiche. Ich habe einen Fehler bei der Zusammenstellung, den ich nicht lösen kann. Vielen Dank im Voraus für jede Hilfe zu diesem Thema. PFB der scala-Code, SBT-Datei, JSON-Datei und Fehler:Parsen Sie eine JSON-Datei in Scala (2.10.1) mit Play
json_example.scala
import scala.io.Source
import play.api.libs.json._
import play.api.libs.json._
object Test extends App {
val line :String = "Foo";
val filename = "users.json"
for (line <- Source.fromFile(filename).getLines().mkString) {
println(line);
val json: JsValue = Json.parse(line);
}
}
JSON-Datei (users.json)
{"users":[
{"ID":"1","firstName":"John", "lastName":"Doe"},
{"ID":"2","firstName":"Anna", "lastName":"Smith"},
{"ID":"3","firstName":"Peter", "lastName":"Jones"}
{"ID":"1","firstName":"Stewie", "lastName":"Doe"},
{"ID":"2","firstName":"Chris", "lastName":"Smith"},
{"ID":"3","firstName":"Louis", "lastName":"Jones"}
{"ID":"2","firstName":"Brian", "lastName":"Smith"},
{"ID":"3","firstName":"Meg", "lastName":"Jones"}
]}
SBT-Datei (einfach .sbt)
lazy val root = (project in file(".")).
settings(
name := "JSON_GRAPHX",
version := "1.0",
scalaVersion := "2.10.1",
libraryDependencies ++= Seq("com.github.scala-incubator.io" %% "scala-io-file" % "0.4.2",
"com.typesafe.play" %% "play-json" % "2.3.4"),
resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/"
)
Fehler
[info] Set current project to JSON_GRAPHX (in build file:/F:/Graphx_app/JSON_GRAPHX/)
[info] Compiling 1 Scala source to F:\Graphx_app\JSON_GRAPHX\target\scala-2.10\classes...
[error] F:\Graphx_app\JSON_GRAPHX\json_example.scala:15: overloaded method value parse with alternatives:
[error] (input: Array[Byte])play.api.libs.json.JsValue <and>
[error] (input: String)play.api.libs.json.JsValue
[error] cannot be applied to (Char)
[error] val json: JsValue = Json.parse(line);
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed Aug 18, 2016 8:51:22 PM
Dies ist eine perfekte Lösung. Frage mich, warum das nicht akzeptiert wurde – Rakshith
Es hat funktioniert. Vielen Dank für die Hilfe @virsox. Entschuldigung für die verspätete Antwort. – Anwesha