看个例子
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.functional.syntax._
object Application extends Controller {
/*1.import play.api.libs.functional.syntax._
case class Contact(email:Option[String],fax:Option[String],phone:Option[String])
case class Company(name:String,contactDetail:Contact)
case class Product(ean:Long,name:String,description:Option[String],pieces:Option[Int],manufactures:Company,tags:List[String],active:Boolean)
implicit val companyReads: Reads[Company] = (
(JsPath \ "name").read[String] andThen
(JsPath \ "contact_details").read(
(
(JsPath \ "email").readNullable[String] andThen
(JsPath \ "fax").readNullable[String] andThen
(JsPath \ "phone").readNullable[String]
)(Contact.apply _))
)(Company.apply _)
implicit val productReads: Reads[Product] = (
(JsPath \ "ean").read[Long] andThen
(JsPath \ "name").read[String] andThen
(JsPath \ "description").readNullable[String] andThen
(JsPath \ "pieces").readNullable[Int] andThen
(JsPath \ "manufacturer").read[Company] andThen
(JsPath \ "tags").read[List[String]] andThen
(JsPath \ "active").read[Boolean]
)(Product.apply _)*/
/*2.val json:JsValue = JsObject(
Seq("name" -> JsString("Watership Down"),
"location" -> JsObject(Seq("lat" -> JsNumber(32.1233),"long" -> JsNumber(-1.309197))),
"residents" -> JsArray(Seq(
JsObject(Seq(
"name" -> JsString("Fiver"),
"age" -> JsNumber(4),
"role" -> JsNull
)),
JsObject(Seq(
"name" -> JsString("Bigwig"),
"age" -> JsNumber(6),
"role" -> JsString("Owsla")
))
)
)
))*/
/*case class Location(lat: Double, long: Double)
case class Resident(name: String, age: Int, role: Option[String])
case class Place(name: String, location: Location, residents: Seq[Resident])*/
/*3.implicit val locationWrites = new Writes[Location]{
def writes(location:Location) = Json.obj(
"lat" -> location.lat,
"long" -> location.long
)
}
implicit val residentWrites = new Writes[Resident]{
def writes(resident:Resident) = Json.obj(