Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another alternatie using Lift-json could be:</p> <pre><code>package code.json import org.specs2.mutable.Specification import net.liftweb.json._ class JsonSpecs extends Specification { implicit val format = DefaultFormats val a = parse("""{ | "id": 100, | "text": "Hello, world." | "user": { | "name": "Brett", | "id": 200 | }, | "geo": { | "lat": 10.5, | "lng": 20.7 | } |}""".stripMargin) val b = parse("""{ | "id": 100, | "text": "Hello, world." | "user": { | "name": "Brett", | "id": 200 | } |}""".stripMargin) "Lift Json" should{ "find the id" in { val res= (a \ "id").extract[String] res must_== "100" } "find the name" in{ val res= (a \ "user" \ "name").extract[String] res must_== "Brett" } "find an optional geo data" in { val res= (a \ "geo" \ "lat").extract[Option[Double]] res must_== Some(10.5) } "ignore missing geo data" in { val res= (b \ "geo" \ "lat").extract[Option[Double]] res must_== None } } } </code></pre> <p>Note how when the geo data is missing on the val b, the parsing works just fine, expecting a None.</p> <p>Or do you want to get case classes as the result?</p> <p>For a case class example, see:</p> <p>package code.json</p> <pre><code>import org.specs2.mutable.Specification import net.liftweb.json._ class JsonSpecs extends Specification { implicit val format = DefaultFormats case class Root(id: Int, text: Option[String], user: Option[User], geo: Option[Geo]) case class User(name: String, id: Int) case class Geo(lat: Double, lng: Double) val c = parse("""{ | "id": 100 | "user": { | "name": "Brett", | "id": 200 | }, | "geo": { | "lng": 20.7 | } |}""".stripMargin) "Lift Json" should{ "return none for geo lat data" in { val res= c.extract[Root].geo.map(_.lat) res must_== None } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload