Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse a json to a `ProvinceJson` class, with lift-json

Tags:

json

scala

lift

Scala code:

import net.liftweb.json._

case class Province(id: String, name: String, parentName: Option[String], parentId: Option[String])

case class ProvinceJson(provinceData: List[Province])

object Test extends Application {
  val json = """ {
               |    "provinceData":
               |        [
               |            {
               |                "name":"hb",
               |                "parentName":null,
               |                "parentId":null,
               |                "id":"450"
               |            }
               |        ]
               |}
               | """.stripMargin

  parse(json).extract[ProvinceJson]

}

When I run this code, it reports compilation error:

could not find implicit value for parameter formats: net.liftweb.json.Formats
parse(json).extract[ProvinceJson]
                    ^

not enough arguments for method extract: (implicit formats: net.liftweb.json.Formats, implicit mf: scala.reflect.Manifest[com.thoughtworks.sfexpress.sf_ws.ProvinceJson])com.thoughtworks.sfexpress.sf_ws.ProvinceJson.
Unspecified value parameters formats, mf.
parse(json).extract[ProvinceJson]
                    ^

Do I miss anything?

like image 694
Freewind Avatar asked May 30 '13 04:05

Freewind


1 Answers

You need to tell the parser which formats to use, and to do that the parser looks for an implicit argument. Adding this somewhere in your code should fix things for you:

implicit val formats = net.liftweb.json.DefaultFormats
like image 98
jcern Avatar answered Oct 16 '22 11:10

jcern