Skip to content

Instantly share code, notes, and snippets.

@leewin12
Last active December 19, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leewin12/5956083 to your computer and use it in GitHub Desktop.
Save leewin12/5956083 to your computer and use it in GitHub Desktop.
Play2 post json string and return its value
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