本文来自: http://fair-jm.iteye.com/ 转截请注明出处
一不小心 一个月没写文了 这个笔记整理也一直搁了好久...哎...
lz实习也稳定下来 现在就做做小需求啊什么的~
最近lz也办了开源力量一年的会员打算学点东西 届时会有些笔记之类的~~
JSON处理主要用到:
play.api.libs.json
包中的对象和类
JsValue及子类:
■ JsString
■ JsNumber Int,Long,Double 有隐式转换
■ JsBoolean
■ JsObject Seq[(String,JsValue)]
■ JsArray Seq[JsValue]做参数
■ JsNull
以上这些代表了json的组成元素 可以直接用以上的类来构建JSON
Json对象提供了一些方便的工具
toJson方法
val product = Json.obj( "name" -> JsString("Blue Paper clips"), "ean" -> JsString("12345432123"), "description" -> JsString("Big box of paper clips"), "pieces" -> JsNumber(500), "manufacturer" -> Json.obj( "name" -> JsString("Paperclipfactory Inc."), "contact_details" -> Json.obj( "email" -> JsString("contact@paperclipfactory.example.com"), "fax" -> JsNull, "phone" -> JsString("+12345654321") ) ), "tags" -> Json.arr( JsString("paperclip"), JsString("coated") ), "active" -> JsBoolean(true) )
如果想将JsValue(及子类对象)转化为String
可以用:
val productJsonString = Json.stringify(product) //用JsValue的toString方法也可以 会调用这个方法
对象到JSON:
toJson方法可用于 String Option[Int] 以及 Map[String,String]
要实现其他类别的 可以继承Writes覆写writes方法
对于case class 也可以用Json提供的方法在运行时生成相应的Writes:
implicit val productWrites = Json.writes[Product]
也可以用Path
JSON到Object:
前者是Writes 现在是Reads
用JsValue的as方法 Reads由JsPath生成:
此外也可以使用asOpt 那么返回的将是一个Option
implicit val reads:Reads[Product] = ( (JsPath \ "ean").read[Long] and (JsPath \ "name").read[String] and (JsPath \ "description").read[String] )(Product.apply _)
(用JsPath也可以生成Writes 把read换成write即可)
从String->JsValue 用Json.parse
在request中也可以用request.body.asJson 得到一个Option[JsValue]
如果Action只处理JSON的数据 可以写成:
def postProduct2() = Action(parse.json) { request => val jsValue = request.body // Do something with the JSON }
如果content-type不符合(text/json 或者 application/json)就会直接返回400 Bad Request 可以使用parse.tolerantJson如果没有设置content-type的话
用jsValue的validate方法可以检查是否可以转换(用隐式的Reads作为参数):
val age = jsValue.validate[Int] // == JsError val name = jsValue.validate[String] // == JsSuccess(Johnny,) import Json._ val json: JsValue = toJson(Map( "name" -> toJson("Johnny"), "age" -> toJson(42), "tags" -> toJson(List("constructor", "builder")), "company" -> toJson(Map( "name" -> toJson("Constructors Inc.")))))
处理:
val name = (json \ "name").as[String] val age = (json \ "age").asOpt[Int] val companyName = (json \ "company" \ "name").as[String] val firstTag = (json \ "tags")(0).as[String] val allNames = (json \\ "name").map(_.as[String])
如果不符合 不会抛出异常 会返回JsUndefined(也是JsValue的子类) 或者也可以用asOpt返回Option
可以用模式匹配处理:
(json \ "name") match { case JsString(name) => println(name) case JsUndefined(error) => println(error) case _ => println("Invalid type!") }
Format
Reads和Writes可以合并 用Format
Format[T]继承自Reads[T]和Writes[T]
然后使用JsPath的format和formatNullable方法:
import play.api.libs.json._ import play.api.libs.functional.syntax._ implicit val productFormat = ( (JsPath \ "name").format[String] and (JsPath \ "description").formatNullable[String] and (JsPath \ "purchase_price").format[BigDecimal] and (JsPath \ "selling_price").format[BigDecimal] )(PricedProduct.apply, unlift(PricedProduct.unapply))
或者用现有的Reads和Writes:
implicit val productFormat = Format(productReads, productWrites)
对于case class 也可以用运行时生成的方式:
implicit val productFormat = Json.format[PricedProduct]
这种方式是用scala macro生成的 类型安全 对应的还有Json.reads()和Json.writes
检查
检查: 可以在reads中写规则 这些规则来自:play.api.libs.json.ConstraintReads
implicit val companyReads: Reads[Company] = ( (JsPath \ "name").read[String] and (JsPath \ "contact_details").read( ( (JsPath \ "email").readNullable[String](email) and (JsPath \ "fax").readNullable[String](minLength[String](10)) and (JsPath \ "phone").readNullable[String](minLength[String](10)) )(Contact.apply _)) )(Company.apply _)
验证用JsValue的validate方法 和Form的bindFromRequest类似:
def save = Action(parse.json) { implicit request => val json = request.body json.validate[Product].fold( valid = { product => Product.save(product) Ok("Saved") }, invalid = { errors => BadRequest(JsError.toFlatJson(errors)) }) }
相关推荐
Play Framework最新教程(12年),play框架教程
play framework api,play! framework api,play api
play2-war-plugin, 用于 Play Framework 2.x的WAR插件 用于 Play Framework 2.x的 WAR插件Current versions: Play 2.2.x : 1.2.1 Play 2.3.0 -> 2.3.1 : 1.3-beta1 (Scala 2.1
Play Framework Cookbook.pdf
1. Play Framework 介绍 2. 创建和发布 Play 应用 2.1 创建 Play 的工程 2.2 Play 常用指令 2.3 Play 应用的 JVM 调优 3. 如何读取静态资源 4. Play框架的配置文件 5. 使用 Play 框架开发 Java 应用 5.1 HTTP...
playframework javaweb playframework javaweb
Mastering Play Framework for Scala
play framework的一些学习总结,基本概念理解,常用的学习方法等。
Playframework 1.2.7 sdk zip包
play framework2.01上半部分。
Play framework框架
PlayFramework框架验证.pdf
Play Framework Cookbook
通过简单的代码和思路写了play framework的简单的小例子,包括添加页面进行的routes配置,页面跳转,页面之间的数据传递等方面。暂时未使用h2内置数据库及ArrayList、HashMap在页面之间的数据传递。
Play Framework Essentials 英文原版,学习 play 框架的最佳入门。
PlayFramework框架安全模块.pdf
Leverage the awesome features of Play Framework to build scalable, resilient, and responsive applications First published: May 2015 274page
Play框架肩负了臃肿的企业级java ee规范,易restful为目标专注于开发效率,是java敏捷开发的最佳参考方案
play framework2.01 下半部分
Play Framework Cookbook playframework,play! 书中范例代码下载地址: https://github.com/spinscale/play-cookbook 292 pages Publisher: Packt Publishing (September 4, 2011) Language: English ISBN-10: ...