Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object play not found in scala application

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?

This is how I import,

import play.api.libs.json._

Post picture how I create the project.

enter image description here

enter image description here

regards, Lin

like image 887
Lin Ma Avatar asked Jan 21 '26 12:01

Lin Ma


1 Answers

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"

and run

$ sbt update

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

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)
  }
}

You will be better off learning basic stuff about SBT at http://www.scala-sbt.org/0.13/docs/index.html

like image 86
mmizutani Avatar answered Jan 23 '26 21:01

mmizutani