问题描述
我正在使用 Eclipse 并创建一个新的 Scala 对象,想使用 play 的 json 解析功能并导入此包,但出现找不到对象 play 的错误.想知道如何在 Scala 对象中使用播放库?
I am using Eclipse and create a new Scala object, want to use json parsing feature of play and import this package, but there is error object play cannot be found. Wondering how to use play library in a Scala object?
这就是我导入的方式,
import play.api.libs.json._
发布我如何创建项目的图片.
Post picture how I create the project.
问候,林
推荐答案
要在普通 Scala 项目中使用 Play 的 Scala Json 库,而不是 Play 项目,需要在 build.sbt
或 project/Build.scala
:
To use Play's Scala Json library in an ordinary scala project, not a Play project, you need to import the library in build.sbt
or project/Build.scala
:
libraryDependencies += "com.typesafe.play" % "play-json_2.11" % "2.5.2"
然后运行
$ sbt update
这会指示 SBT 从远程 Maven 存储库中获取 Scala 库 play-json
.上面的行与存储库查看器页面的SBT"选项卡上的相同:http://mvnrepository/artifact/com.typesafe.play/play-json_2.11/2.5.2#sbt
This instructs the SBT to fetch the scala library play-json
from a remote Maven repository. The line above is the same as is found on the "SBT" tab of the repository viewer page: http://mvnrepository/artifact/com.typesafe.play/play-json_2.11/2.5.2#sbt
既然您已经将库添加到您的项目中,您可以将其导入并在您的代码中使用,例如 src/main/scala/com/example/Hello.scala
:
Now that you have added the library into your project, you can import and use it in your code such as src/main/scala/com/example/Hello.scala
:
package com.example
import play.api.libs.json._
object Hello {
def main(args: Array[String]): Unit = {
val json: JsValue = Json.parse("""
{
"name" : "Watership Down",
"location" : {
"lat" : 51.235685,
"long" : -1.309197
},
"residents" : [ {
"name" : "Fiver",
"age" : 4,
"role" : null
}, {
"name" : "Bigwig",
"age" : 6,
"role" : "Owsla"
} ]
}
""")
println(json)
}
}
您最好在 http://www 上学习有关 SBT 的基本知识.scala-sbt/0.13/docs/index.html
这篇关于在 Scala 应用程序中找不到对象播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论