gpt4 book ai didi

scala - 在 Scala 应用程序中找不到对象 Play

转载 作者:行者123 更新时间:2023-12-04 18:00:06 25 4
gpt4 key购买 nike

我正在使用 Eclipse 并创建一个新的 Scala 对象,想使用 play 的 json 解析功能并导入此包,但是出现无法找到 play 对象的错误。想知道如何在 Scala 对象中使用 Play 库?

我是这样导入的,

import play.api.libs.json._

张贴我如何创建项目的图片。

enter image description here

enter image description here

问候,林

最佳答案

要在普通scala项目而不是Play项目中使用Play的Scala Json库,需要在build.sbtproject/Build.scala中导入该库:

libraryDependencies += "com.typesafe.play" % "play-json_2.11" % "2.5.2"

然后运行

$ sbt update

这指示 SBT 从远程 Maven 存储库中获取 scala 库 play-json。上面的行与存储库查看器页面的“SBT”选项卡上的相同:http://mvnrepository.com/artifact/com.typesafe.play/play-json_2.11/2.5.2#sbt

现在您已经将库添加到您的项目中,您可以在您的代码中导入和使用它,例如 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.scala-sbt.org/0.13/docs/index.html 上学习有关 SBT 的基础知识会更好

关于scala - 在 Scala 应用程序中找不到对象 Play ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com