我有一个这样的JSON结果:
[
{
"id": 2202,
"name": "name one",
"phone": "+62888888",
"email": "customer@gmail.com",
"corporateId": null,
"corporateName": null,
"version": 119,
"signupDate": "2016-01-28T00:00:00+07:00"
},
{
"id": 2580,
"name": "name two",
"phone": "+628777777",
"email": "customer2@gmail.com",
"corporateId": null,
"corporateName": null,
"version": 119,
"signupDate": "2016-01-28T00:00:00+07:00"
}
]
如何删除一些元素,以便我有这样的新 JSON 结果(删除corporateId
和corporateName
):
[
{
"id": 2202,
"name": "name one",
"phone": "+62888888",
"email": "customer@gmail.com",
"version": 119,
"signupDate": "2016-01-28T00:00:00+07:00"
},
{
"id": 2580,
"name": "name two",
"phone": "+628777777",
"email": "customer2@gmail.com",
"version": 119,
"signupDate": "2016-01-28T00:00:00+07:00"
}
]
有一些方法。其中一个是JSON转换器。
要删除corporateId
和corporateName
用例 6:从输入 JSON 中修剪分支:
(__ "corporateId").json.prune andThen (__ "corporateName").json.prune
要删除每个 JsArray 元素,请使用 Reads.list
:
Reads.list(
(__ "corporateId").json.prune andThen (__ "corporateName").json.prune
)
适合您的复杂变压器将是:
json.transform(
Reads.list(
(__ "corporateId").json.prune andThen (__ "corporateName").json.prune
).map(JsArray)
)
res3: play.api.libs.json.JsResult[play.api.libs.json.JsArray] = JsSuccess([{"id":2202,"name":"name one","phone":"+62888888","email":"customer@gmail.com","version":119,"signupDate":"2016-01-28T00:00:00+07:00"},{"id":2580,"name":"name two","phone":"+628777777","email":"customer2@gmail.com","version":119,"signupDate":"2016-01-28T00:00:00+07:00"}],)