Reputation: 39
I have to the code:
socket.emit("login",obj, new Ack {
override def call(args: AnyRef*): Unit = {
println(args)
}
})
Console output
WrappedArray({"uid":989,"APILevel":5,"status":"ok"})
How to convert args from WrappedArray to JSON?
Upvotes: 0
Views: 129
Reputation: 8529
There are many json libraries. For example you can take a look at Scala json parsers performance, to see the usage and performance. I'll demonstrate how that can be done using play-json. We need to first create a case class that represents your data model:
case class Model(uid: Int, APILevel: Int, status: String)
Now we need to create a formatter, on the companion object:
object Model {
implicit val format: Format[Model] = Json.format[Model]
}
To create a JsValue from it, you can:
val input: String = "{\"uid\":989,\"APILevel\":5,\"status\":\"ok\"}"
val json: JsValue = Json.parse(input)
and to convert it to the model:
val model: Model = json.as[Model]
A complete running example can be found at Scastie. Just don't forget to add play-json as a dependency, by adding the following to your build.sbt
:
resolvers += "play-json" at "https://mvnrepository.com/artifact/com.typesafe.play/play-json"
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.1"
Upvotes: 1