[英]play framework 2 with scala: pretify json
我正在使用Play框架2對json的本機支持(http://www.playframework.org/documentation/latest/ScalaJson),並且我有一個JsValue,我將其轉換為字符串以將其保存到文本文件中,例如這個
val json: JsValue = [....]
Json.stringify(json)
哪個工作正常,但會生成如下內容:
{"tokens":[{"id":"1000","token":"DON...
我想知道是否有一種簡單的方法來生成這樣的格式化json
{
"tokens":
[
{
"id":"1000",
"token":"DON...
我使用了liftweb JSON包及其Printer
對象/特征。 效果很好:
import play.api._
import play.api.mvc._
import play.api.libs._, concurrent._, json._
import com.mongodb.casbah.Imports.{MongoConnection, MongoCursor, WriteConcern}
import com.mongodb.casbah.query.Imports._
import com.novus.salat.json._
import net.liftweb.json.{render => jsonRender, _}
class Application extends Controller {
def getJson(id: String) = Action { implicit request =>
val objPromise = Akka.future(Database.getById(id))
Async {
objPromise.orTimeout("Error", 1000).map { o =>
o.fold(
hit => Ok(hit.map{ o: DBObject => pretty(jsonRender(ToJValue(o)))}.getOrElse("")).as("text/json"),
timeout => InternalServerError(timeout)
)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.