-1

使用 play framework / scala 创建 Json 很容易。这是一个关于它的例子,

 import play.api.libs.json.Json

val json: JsValue = Json.parse("""
{ 
  "user": {
    "name" : "toto",
    "age" : 25,
    "email" : "toto@jmail.com",
    "isAlive" : true,
    "friend" : {
      "name" : "tata",
      "age" : 20,
      "email" : "tata@coldmail.com"
    }
  } 
}
""")

欲了解更多信息和来源:http ://www.playframework.com/documentation/2.1.1/ScalaJson

4

1 回答 1

3

我猜你只想要一个数组,而不是一个对象:

Json.arr(
  Json.obj(
    "id" -> "node37",
    "name" -> 3.7,
    "data" -> Json.obj(),
    "children" -> Json.arr()
  ),
  Json.obj(
    "id" -> "node38",
    "name" -> 3.8,
    "data" -> Json.obj(),
    "children" -> Json.arr()
  )
)

这使

 [{
      "id": "node37",
      "name": "3.7",
      "data": {},
      "children":[]
 },
 {
      "id": "node38",
      "name": "3.8",
      "data": {},
      "children":[]
 }]
于 2013-05-13T13:16:38.573 回答