使用 scala play 将 Json 对象添加到 JSON 数组
Add a Json object to JSON Array using scala play
这是我现在的 json:
{"name":"James",
"child": {"id":1234,"name":"Ruth",
"grandchild":{"id":1111,"name":"Peter"}
}
}
我想做成这样:
{"name":"James",
"child": [{"id":1234,"name":"Ruth",
"grandChild":[{"id":1111,"name":"Peter"}]
}]
}
代码如下:
def getParentJSON = {
Json.obj(
"name"->"James",
"child"->getChildJson
)
}
def getChildJSON = {
Json.obj(
"id"->"1234",
"name"->"Ruth",
"grandChild"->getGrandChildJson
)
}
def getGrandChildJSON = {
Json.obj(
"id"->"1111",
"name"->"Peter"
)
}
我尝试使用 JsArray.append(getParentJSON)。
但是没有用。
任何帮助将不胜感激。
谢谢
使用Json.arr
:
def getParentJSON = {
Json.obj(
"name" -> "James",
"child" -> Json.arr(getChildJSON)
)
}
def getChildJSON = {
Json.obj(
"id" -> "1234",
"name" -> "Ruth",
"grandChild" -> Json.arr(getGrandChildJSON)
)
}
这是我现在的 json:
{"name":"James",
"child": {"id":1234,"name":"Ruth",
"grandchild":{"id":1111,"name":"Peter"}
}
}
我想做成这样:
{"name":"James",
"child": [{"id":1234,"name":"Ruth",
"grandChild":[{"id":1111,"name":"Peter"}]
}]
}
代码如下:
def getParentJSON = {
Json.obj(
"name"->"James",
"child"->getChildJson
)
}
def getChildJSON = {
Json.obj(
"id"->"1234",
"name"->"Ruth",
"grandChild"->getGrandChildJson
)
}
def getGrandChildJSON = {
Json.obj(
"id"->"1111",
"name"->"Peter"
)
}
我尝试使用 JsArray.append(getParentJSON)。 但是没有用。
任何帮助将不胜感激。
谢谢
使用Json.arr
:
def getParentJSON = {
Json.obj(
"name" -> "James",
"child" -> Json.arr(getChildJSON)
)
}
def getChildJSON = {
Json.obj(
"id" -> "1234",
"name" -> "Ruth",
"grandChild" -> Json.arr(getGrandChildJSON)
)
}