在scala应用程序中找不到对象播放 [英] object play not found in scala application
问题描述
我正在使用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项目)中使用Play的Scala Json库,您需要在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"选项卡上的行相同:
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.com/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.org/0.13/docs/index.html
这篇关于在scala应用程序中找不到对象播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!