I have a data with Map Structure. I want to iterate on the data and extract a list of keys with special values. I wrote the below code:
val jsonString = os.read(os.pwd/"src"/"main"/"scala"/"config.json")
val data = ujson.read(jsonString)
for ((k,v) <- data) println(s"key: $k, value: $v")
But when I run it, received this error:
Can not resolve foreach symbol
Also, when I wrote run
in sbt shell
; it show me this error:
[error] /home/spark/scala_project/test/src/main/scala/main.scala:99:17: value withFilter is not a member of ujson.Value.Value
[error] for ((k,v) <- data) println(s"key: $k, value: $v")
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
I added this dependency to build.sbt. But, it did n't solve the problem.
libraryDependencies += "com.foreach.across" % "across-core" % "5.1.3.RELEASE"
Would you please guide me how to solve the issue?
Any help is really appreciated.
Not sure why you decided to add "com.foreach.across" % "across-core"
. This is something for Spring
Across is a Java framework that aims to facilitate module based development for Java (web) applications. It builds heavily on Spring framework and allows defining a module consisting of a number of classes and configuration files. Every module defines its own Spring application context and can share one or more beans with other modules. https://mvnrepository.com/artifact/com.foreach.across/across-core
You didn't specify what json library you're using. I suspect that it's Li Haoyi's uJson
https://www.lihaoyi.com/post/uJsonfastflexibleandintuitiveJSONforScala.html, which is the back-end for his upickle
https://github.com/com-lihaoyi/upickle https://com-lihaoyi.github.io/upickle/
Try
val m = data.asInstanceOf[ujson.Obj].value
for ((k, v) <- m) println(s"key: $k, value: $v")
or
data match {
case ujson.Obj(m) => for ((k, v) <- m) println(s"key: $k, value: $v")
}