Last active
December 19, 2015 12:39
-
-
Save leewin12/5956083 to your computer and use it in GitHub Desktop.
Play2 post json string and return its value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class TestObj(name: String, msg: String) | |
def insertMulti() = Action { | |
implicit req => | |
// parse posted json objs | |
val json = req.body.asJson.getOrElse(JsNull) | |
val objs = json.as[List[JsValue]].map { | |
json => Json.reads[TestObj].reads(json).get | |
} | |
// return what we got as json string | |
Ok(JsArray(objs map { | |
obj => Json.writes[TestObj].writes(obj) | |
})) | |
} | |
def insertSingle() = Action { | |
implicit req => | |
// parse posted json obj | |
val obj = Json.reads[TestObj].reads(req.body.asJson.getOrElse(JsNull)) | |
// return what we got as json string | |
Ok(Json.writes[TestObj].writes(obj.getOrElse(null))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment