如何使用JSON变换器在Play上展平js数组?

时间:2014-02-20 12:12:04

标签: json scala playframework

我有一个以下格式的Json:

  {
    "user": {
      "id": "1",
      "name": "Some User",
      "permGroups": [
        {
          "id": "group1",
          "name": "Group 1",
          "actions": [
            {
              "id": "action1",
              "name": "Action 1"
            }
          ]
        },
        {
          "id": "group2",
          "name": "Group 2",
          "actions": [
            {
              "id": "action2",
              "name": "Action 2"
            },
            {
              "id": "action3",
              "name": "Action 3"
            }
          ]
        }
      ]
    },
    "title": "New Role",
    "role_id": "56fea66c"
  }

如何在Play中制作JSON Transformer! 2.1,将其变为:

{
 "name": "New Role",
 "id" : "56fea66c",
 "permGroupIds": ["group1","group2"]
 "actions": ["action1", "action2", "action3"]
}

到目前为止我有这个工作:

import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._

val jsonStr = """{"user":{"id":"1","name":"Some User","permGroups":[{"id":"group1","name":"Group 1","actions":[{"id":"action1","name":"Action 1"}]},{"id":"group2","name":"Group 2","actions":[{"id":"action3","name":"Action 3"},{"id":"action3","name":"Action 3"}]}]},"title":"New Role","role_id":"56fea66c"}"""
val jsonVal = Json.parse(jsonStr)

val jsonTransformer = (
    (__ \ 'name).json.copyFrom((__ \ 'title).json.pick) and
    (__ \ 'id).json.copyFrom((__ \ 'role_id).json.pick)
  ).reduce

jsonVal.transform(jsonTransformer)

这会产生:

{
 "name": "New Role",
 "id" : "56fea66c",
}

1 个答案:

答案 0 :(得分:1)

这是一种方法。这将忽略任何不具有ID的烫发组或操作。

<div class="parent">
  <div class="child">
    <div class="g-child"></div>
  </div>
</div>

.parent {
  width: 350px;
  height: 350px;
  background: rebeccapurple;
  margin: 1em auto;
 position: relative;
}

.child {
  position: absolute;
  width: 50px;
  height: 50px;
  right: 50px;
  top: 50px;
  background: orange;
}

.g-child {
  position: absolute;
  width: 25px;
  height: 25px;
  background: #f00;
  top:125%;
  right: 0;
}

这会产生

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

val jsonStr = """{"user":{"id":"1","name":"Some User","permGroups":[{"id":"group1","name":"Group 1","actions":[{"id":"action1","name":"Action 1"}]},{"id":"group2","name":"Group 2","actions":[{"id":"action3","name":"Action 3"},{"id":"action3","name":"Action 3"}]}]},"title":"New Role","role_id":"56fea66c"}"""

val jsonTransformer = (
  (__ \ 'name).json.copyFrom((__ \ 'user \ 'name).json.pick) and
  (__ \ 'id).json.copyFrom((__ \ 'role_id).json.pick) and
  (__ \ 'permGroupIds).json.copyFrom((__ \ 'user \ 'permGroups).read[List[JsObject]].map(permGroups =>
    JsArray(permGroups.flatMap(permGroup => (permGroup \ "id").toOption))
  )) and
  (__ \ 'actions).json.copyFrom((__ \ 'user \ 'permGroups).read[List[JsObject]].map(permGroups =>
    JsArray(permGroups.flatMap(permGroup => (permGroup \ "actions").asOpt[JsArray]).flatMap(_ \\ "id"))
  ))
).reduce

val transformed = Json.parse(jsonStr).transform(jsonTransformer)
Json.prettyPrint(transformed.get)