Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Scala如何从数组转换为JSON格式_Arrays_Json_Scala_Socket.io - Fatal编程技术网

Arrays Scala如何从数组转换为JSON格式

Arrays Scala如何从数组转换为JSON格式,arrays,json,scala,socket.io,Arrays,Json,Scala,Socket.io,我必须遵守守则: socket.emit("login",obj, new Ack { override def call(args: AnyRef*): Unit = { println(args) } }) 控制台输出 WrappedArray({"uid":989,"APILevel":5,"status":&quo

我必须遵守守则:

socket.emit("login",obj, new Ack {
         override def call(args: AnyRef*): Unit = {           
           println(args)
         }
       })
控制台输出

WrappedArray({"uid":989,"APILevel":5,"status":"ok"})

如何将参数从WrappedArray转换为JSON?

有许多JSON库。例如,您可以查看,以了解使用情况和性能。我将演示如何使用。我们需要首先创建一个代表您的数据模型的case类:

case class Model(uid: Int, APILevel: Int, status: String)
现在,我们需要在上创建一个格式化程序:

要从中创建一个文件,您可以:

val input: String = "{\"uid\":989,\"APILevel\":5,\"status\":\"ok\"}"
val json: JsValue = Json.parse(input)
并将其转换为模型:

val model: Model = json.as[Model]
有关完整的运行示例,请访问。只是不要忘记添加play json作为依赖项,方法是将以下内容添加到
build.sbt

resolvers += "play-json" at "https://mvnrepository.com/artifact/com.typesafe.play/play-json"
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.1"

使用json库?可以举个例子吗?@pbezpal请阅读
resolvers += "play-json" at "https://mvnrepository.com/artifact/com.typesafe.play/play-json"
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.1"