首页 文章

在scala应用程序中找不到对象播放

提问于
浏览
2

我正在使用Eclipse并创建一个新的Scala对象,想要使用json解析功能并导入这个包,但是找不到错误对象播放 . 想知道如何在Scala对象中使用play库?

这是我导入的方式,

import play.api.libs.json._

发布图片我是如何创建项目的 .

enter image description here

enter image description here

问候,林

1 回答

  • 1

    要在普通的scala项目中使用Play的Scala Json库,而不是Play项目,您需要在 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的基本知识

相关问题