play json 操作
所属分类 play
浏览量 723
https://www.playframework.com/documentation/2.8.x/ScalaJson
JSON automated mapping
https://www.playframework.com/documentation/2.8.x/ScalaJsonAutomated
play-json_2.13-2.8.2.jar
import play.api.libs.json._
case class Pet(id: Int, name: String, age: Int);
val pet = Pet(1, "cat", 3)
implicit val petReads: Reads[Pet] = Json.reads[Pet]
implicit val petWrites: OWrites[Pet] = Json.writes[Pet]
val petJsValue: JsValue = Json.toJson(pet)(petWrites)
println("petJsValue=" + petJsValue);
val str = Json.stringify(petJsValue)
println(str);
// 使用隐式变量
val petJsValue2: JsValue = Json.toJson(pet)
println("petJsValue2=" + petJsValue2);
val petJsResult: JsResult[Pet] = Json.fromJson[Pet](petJsValue)(petReads)
println("petJsResult=" + petJsResult);
val pet2: Pet = petJsResult.getOrElse(null)
println(pet2);
// 使用隐式变量
val petJsResult2 = Json.fromJson[Pet](petJsValue)
完整代码
https://gitee.com/dyyx/hellocode/blob/master/demo/scala/play-rest-hello/app/test/JsonDemo.scala
上一篇
下一篇
依赖注入 guice 实例
scala 多行字符串
scala 元组和多重赋值
play Application Settings
akka hello http server
scala中的classOf isInstanceOf asInstanceOf