저는 스칼라 / 플레이의 초보자이고 JSON 의 query
배열 을 업데이트 (새 ID 추가)하려고 시도 sections[1]
했지만 변압기와 방법에 대한 지식이 거의 없기 때문에 JSON을 통과하는 데 성공하지 못했습니다. 그것을 사용합니다.
"definitions": [
{
"sections": [
{
"priority": 1,
"content": {
"title": "Driver",
"links": [
{
"url": "https://blabla.com",
"text": "See all"
}
]
},
"SearchQuery": {
"options": {
"aggregate": true,
"size": 20,
},
"query": "{\"id\":{\"include\":[\"0wxZ4Nr2\", \"0wxZbNr2\", \"6WZOPMw1\"}}"
}
},
{
"priority": 2,
"content": {
"title": "Deliver",
"links": [
{
"url": "https://blabla.com",
"text": "See all"
}
]
},
"SearchQuery": {
"options": {
"aggregate": true,
"size": 20,
},
"query": "{\"id\":{\"include\":[\"2W12Q2wq\", \"Nwq09lW3\", \"QweNN2d9\"]}}"
}
}
]
}
이것을 달성하는 방법에 대한 제안. 내 목표는 JSON 배열의 특정 필드에 값을 넣는 것입니다. 내 애플리케이션 전체에서 Play JSON 라이브러리를 사용하고 있습니까?
PlayJSON을 사용하면 Json Transformers를 사용할 수 있습니다.
필드 업데이트는 다음과 같이 작동합니다.
val queryUpdater = (__ \ "definitions" \ 1 \ "SearchQuery" \ "query").json.update(
of[JsString].map {
case JsString(value) =>
val newValue: String = ... // calculate new value
JsString(newValue)
}
)
json.transform(queryUpdater)
모든 쿼리를 업데이트해야하는 경우 다음과 같습니다.
val updateQuery = (__ \ "SearchQuery" \ "query").json.update(
of[JsString].map {
case JsString(value) =>
val newValue: String = ... // calculate new value
JsString(newValue)
}
)
val updateQueries = (__ \ "definitions").json.update(
of[JsArray].map {
case JsArray(arr) =>
JsArray(arr.map(_.transform(updateQuery)))
}
)
json.transform(updateQueries)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다