0
給出下面的JSON ...播放2.4.1:如需要如何替換一個或多個鍵的所有出現在JSON樹
{
"id" : "52fe942b790000790079b7d0",
"email" : "[email protected]",
"username" : "joe",
"subscriptions" : [
{
"accountId" : "72fe942b790000790079b755",
"name" : "test 1",
"isDefault" : true
},
{
"accountId" : "72fe942b796850790079b743",
"name" : "test 2",
"isDefault" : false
}
]
}
。我需要每個ID轉換成一個對象ID通過MongoDB的(即id
- >_id \ $oid
和accountId
- >accountId \ $oid
:
{
"_id" : {"$oid" : "52fe942b790000790079b7d0"},
"email" : "[email protected]",
"username" : "joe",
"subscriptions" : [
{
"accountId" : {"$oid" : "72fe942b790000790079b755"},
"name" : "test 1",
"isDefault" : true
},
{
"accountId" : {"$oid" : "72fe942b796850790079b743"},
"name" : "test 2",
"isDefault" : false
}
]
}
行動中發揮2.3.8我用play-json-zipper
和updateAllKeyNodes
的伎倆:
import play.api.libs.json._
import play.api.libs.json.extensions._
json.updateAllKeyNodes {
case ((_ \ "_id"), value) => "id" -> value \ "$oid"
case ((_ \ "accountId"), value) => "accountId" -> value \ "$oid"
}
不幸的是具有播放2.4.1我必須從我的項目中移除play-json-zipper
因爲它不支持新的JSON模式。
用標準的Play JSON庫實現相同結果的正確方法是什麼?
不,這並不在我的情況下工作,因爲JSON樹是相當巨大的,我也需要改造accountId'的'所有實例中列表。最後,'id'必須轉換爲'{「_id」:{「$ oid」:「123」}}和'accountId'爲'{「accountId」:{「$ oid」:「123」}} '。 – j3d
查看更新的答案 – bjfletcher