scala play处理json

看个例子

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(
	
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值