Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
scala>
scala> import net.liftweb.json._
import net.liftweb.json._
scala>
scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@33462704
scala>
scala>
scala>
scala> case class TypesQueries(queries: Map[String, JValue]);
defined class TypesQueries
scala> case class AddressQuery(street: String, city: String, state: String, zip: Int)
defined class AddressQuery
scala> case class AddressQueries(addresses: List[AddressQuery]);
defined class AddressQueries
scala> case class NameQuery(firstName: String, lastName: String);
defined class NameQuery
scala> case class NameQueries(names: List[NameQuery]);
defined class NameQueries
scala> case class EmailQuery(emailAddress: String);
defined class EmailQuery
scala> case class EmailQueries(emails: List[EmailQuery]);
defined class EmailQueries
scala>
scala> val jsonData = parse("""{
| "queries" : {
| "addresses" : [
| {
| "street" : "1234 Main St.",
| "city" : "New York",
| "state" : "New York",
| "zip" : 12345
| },
| {
| "street" : "9876 Broadway Blvd.",
| "city" : "Chicago",
| "state" : "IL",
| "zip" : 23456
| }
| ],
| "names": [
| {
| "firstName" : "John",
| "lastName" : "Doe"
| }
| ],
| "emails" : [
| {
| "emailAddress" : "john.doe@gmail.com"
| },
| {
| "emailAddress" : "david.smith@gmail.com"
| }
| ]
| }
| }""");
jsonData: net.liftweb.json.package.JValue = JObject(List(JField(queries,JObject(List(JField(addresses,JArray(List(JObject(List(JField(street,JString(1234 Main St.)), JField(city,JString(New York)), JField(state,JString(New York)), JField(zip,JInt(12345)))), JObject(List(JField(street,JString(9876 Broadway Blvd.)), JField(city,JString(Chicago)), JField(state,JString(IL)), JField(zip,JInt(23456))))))), JField(names,JArray(List(JObject(List(JField(firstName,JString(John)), JField(lastName,JString(Doe))))))), JField(emails,JArray(List(JObject(List(JField(emailAddress,JString(john.doe@gmail.com)))), JObject(List(JField(emailAddress,JString(david.smith@gmail.com))))))))))))
scala>
scala>
scala> val typesQuery = parse(jsonData).extract[TypesQueries];
<console>:24: error: type mismatch;
found : net.liftweb.json.package.JValue
required: String
val typesQuery = parse(jsonData).extract[TypesQueries];
^
scala>
scala>
scala> typesQuery.queries.foreach {
| case(queryType, queryDefinition) => queryType match {
| case "addresses" => val addressQueries = typesQuery.queries.extract[AddressQueries]; println(addressQueries)
| case "names" => val nameQueries = typesQuery.queries.extract[NameQueries]; println(nameQueries)
| case "emails" => val emailQueries = typesQuery.queries.extract[EmailQueries]; println(emailQueries)
| }
| }
AddressQueries(List(AddressQuery(1234 Main St.,New York,New York,12345), AddressQuery(9876 Broadway Blvd.,Chicago,IL,23456)))
NameQueries(List(NameQuery(John,Doe)))
EmailQueries(List(EmailQuery(john.doe@gmail.com), EmailQuery(david.smith@gmail.com)))
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.