j3d*_*j3d 3 scala playframework
鉴于以下JSON ......
{
"values" : [
"one",
"two",
"three"
]
}
Run Code Online (Sandbox Code Playgroud)
...如何在Scala/Play中将其转换为这样?
{
"values" : [
{ "elem": "one" },
{ "elem": "two" },
{ "elem": "three" }
]
}
Run Code Online (Sandbox Code Playgroud)
Play的JSON变形金刚很容易:
val json = Json.parse(
"""{
| "somethingOther": 5,
| "values" : [
| "one",
| "two",
| "three"
| ]
|}
""".stripMargin
)
// transform the array of strings to an array of objects
val valuesTransformer = __.read[JsArray].map {
case JsArray(values) =>
JsArray(values.map { e => Json.obj("elem" -> e) })
}
// update the "values" field in the original json
val jsonTransformer = (__ \ 'values).json.update(valuesTransformer)
// carry out the transformation
val transformedJson = json.transform(jsonTransformer)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |