假設以下兩種JSON代碼段:如何命名JsArray轉換成JsObject與JsZipper
{ "include": ["field1", "field2", "fieldN"] }
{ "exclude": ["field1", "field2", "fieldN"] }
我需要轉換include
陣列是這樣的...
{ "field1": 1, "field2": 1, "fieldN": 1 }
...和exclude
陣列是這樣的:
{ "field1": 0, "field2": 0, "fieldN": 0 }
[只爲你的inf ○:在這裏我下面需要輸入JSON轉換爲蒙戈的預測]
是我目前的解決方案–我實現它作爲一個JsValue
擴展:
object testTypeExtensions {
implicit class TestJsExtensions(val json: JsValue) extends AnyVal {
def toProjection: JsValue = {
if (json \\ "include" nonEmpty)
JsObject(for (field <- (json \ "include").as[List[JsString]])
yield (field.value, JsNumber(1)))
else if (json \\ "exclude" nonEmpty)
JsObject(for (field <- (json \ "exclude").as[List[JsString]])
yield (field.value, JsNumber(0)))
else Json.obj()
}
}
}
上面的代碼工作...
scala> val p = Json.obj("exclude" -> Json.arr("field1", "field2"))
p: play.api.libs.json.JsObject = {"exclude":["field1","field2"]}
scala> p.toProjection
res12: play.api.libs.json.JsObject = {"field1":0,"field2":0}
...但我相信它可以寫得更好JsZipper
。
此外,它不是很靈活,因爲它只管理include
和exclude
鍵,而我想還可以管理其他類似的情況下,像選對象:
{ "asc": ["field1", "field2"] }
{ "desc": ["field1", "field2"] }
...轉化成......
{ "field1": 1, "field2": 1 }
...和
{ "field1": -1, "field2": -1 }
這就是說,我所考慮的是經營任何一種名爲JSON陣列的像普通的方法:
object testypeExtensions {
implicit class TempJsExtensions(val json: JsValue) extends AnyVal {
def namedArrayToObject(keys: String*): JsValue = {
// how to implement it, possibly with JsZipper
}
}
}
的namedArrayToObject
方法應該在當前的JSON搜索指定keys
,併產生了一個對象第一場比賽像我在本文開頭描述的那些,可能與JsZipper
。這是對預期結果的模擬。
搜索exclude
和include
並返回第一個匹配的JsObject
:
scala> val p = Json.obj("exclude" -> Json.arr("field1", "field2"))
scala> p.namedArrayToObject("exclude", "include")
res12: play.api.libs.json.JsObject = {"field1":0,"field2":0}
和以前一樣......但現在輸入JSON包含include
而不是exclude
:
scala> val p = Json.obj("include" -> Json.arr("field1", "field2"))
scala> p.namedArrayToObject("exclude", "include")
res12: play.api.libs.json.JsObject = {"field1":1,"field2":1}
搜索asc
和desc
並將第一個匹配返回爲JsObject
:
scala> val p = Json.obj("desc" -> Json.arr("field1", "field2"))
scala> p.namedArrayToObject("asc", "desc")
res12: play.api.libs.json.JsObject = {"field1":-1,"field2":-1}
...等等。
如果沒有匹配,namedArrayToObject
應該返回一個空的JsObject
。任何關於如何以正確的方式實施這一建議將非常感謝。
的Tx爲你的解釋。問題是輸入JSON不能同時包含「include」和「exclude」;如果是這樣,只應該進行第一場比賽。例如,將「示例」JSON作爲輸入,「toObject」應該返回{「field1」:1,「field2」:1,「field3」:3}。這就是爲什麼我正在考慮JsZipper ... – j3d
通過組合'orElse','和Then',和'prune',你可以得到接近這種行爲的東西,但是JSON對象是無序的,所以「_first_match」不會至少不應該)意味着什麼。 –