简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM