0

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?

3

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.