怎么能一发,用Play!框架,格式化为人类可读的 JSON 响应?
例如,我正在寻找类似的东西:
def handleGET(path:String) = Action{ implicit request =>
val json = doSomethingThatReturnsAJson(path,request)
request.getQueryString("pretty") match {
case Some(_) => //some magic that will beautify the response
case None => Ok(json)
}
}
我的搜索使我找到了 JSON pretty-print ,这本身并不是很有帮助,但它确实表示应该在 future 版本中集成该功能。那是 play 2.1.X,所以,我猜它已经存在于 2.2X 版本的 play 中?
最佳答案
Play 框架内置了 pretty-print 支持:
import play.api.libs.json.Json
Json.prettyPrint(aJsValue)
因此,在您的情况下,执行以下操作就足够了:
def handleGET(path:String) = Action { implicit request =>
val json = doSomethingThatReturnsAJson(path, request)
request.getQueryString("pretty") match {
case Some(_) => Ok(Json.prettyPrint(json)).as(ContentTypes.JSON)
case None => Ok(json)
}
}
关于json - 如何使用 Play Framework 响应打印精美的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175723/