2011-07-07 55 views
2

我正在使用lift-json 2.3和Scala 2.8.1來操縱json文檔。在下面的文檔中,我想刪除單個節點。使用lift-json刪除子節點

{ 
    "units": { 
    "1": { 
     "id":"1", 
     "name":"one", 
     "tenants": { 
     "1" : { 
      "id": "1", 
      "name":"x" 
     }, 
     "2" : {  // node I want removed 
      "id": "2", 
      "name":"y" 
     } 
     } 
    } 
    "2": { 
     "id":"2", 
     "name":"two", 
     "tenants": { 
     "1" : { 
      "id": "1", 
      "name":"x" 
     }, 
     "2" : { 
      "id": "2", 
      "name":"z" 
     } 
     } 
    } 
    } 
} 

我知道我可以刪除節點與remove方法:

val js2 = js \ "units" \ "1" \ "tenants" remove { 
    case JField(id, _) => id == "2" 
    case _ => false 
} 

但是,我需要整個文檔改變。

回答

4

刪除是通過將AST節點轉換爲JNothing完成的。有幾個功能可以做到這一點,例如'轉變'。但在這種情況下,它看起來像'替換'效果最好:

js.replace("units" :: "1" :: "tenants" :: "2" :: Nil, JNothing) 
+0

完全錯過了替換方法。正是我需要的。 –