您可以JsValue手动构建JSON对象树(a )
import play.api.libs.json._ val json = JsObject(Map( "name" -> JsString("Jsony McJsonface"), "age" -> JsNumber(18), "hobbies" -> JsArray(Seq( JsString("Fishing"), JsString("Hunting"), JsString("Camping") )) ))
或使用较短的等效语法,基于一些隐式转换:
import play.api.libs.json._ val json = Json.obj( "name" -> "Jsony McJsonface", "age" -> 18, "hobbies" -> Seq( "Fishing", "Hunting", "Camping" ) )
要获取JSON字符串:
json.toString // {"name":"Jsony McJsonface","age":18,"hobbies":["Fishing","Hunting","Camping"]} Json.prettyPrint(json) // { // "name" : "Jsony McJsonface", // "age" : 18, // "hobbies" : [ "Fishing", "Hunting", "Camping" ] // }