我在请求中收到了JSON对象,是否可以在Play2中将某个键替换为另一个键?
更改JSON:
{ "name" : "My name" }例如:
{ "nameAndSurname" : "My name" }我有更复杂的JSON。我可以使用JSON转换器吗?
我使用Scala是一个简单的例子:
val json = Json.obj("name" -> "My name")
json.transform() <======== ???发布于 2014-04-06 13:07:58
我认为这很简单。这里有一个应该会有帮助的例子:
case class User(nameAndSurname: String, age: Int)
implicit val userReads: Reads[User] = (
(JsPath \ "name").read[String] and
(JsPath \ "age").read[Int]
)(User.apply _)
// controller
...
json.validate[User] match {
case s: JsSuccess[User] => {
val user: User = s.get
// do something with user
}
case e: JsError => {
// error handling flow
}}
https://stackoverflow.com/questions/22862899
复制相似问题