scala json4s删除json里面的key
如题,采用json4s,scala删除json里面的key?
比如有这么一段json:{"@type":{"version":"1.0.2","name":"application-content","data":[]},"key-to-remove":[{"blah":"more blah"}],"@value":[]},要删除key-to-remove这个key,采用removeField即可。上代码:
package dev.json import org.json4s.DefaultFormats import org.json4s.JsonDSL._ import org.json4s.jackson.JsonMethods._ object Course2 { implicit val formats = DefaultFormats def main(args: Array[String]): Unit = { val key = "key-to-remove" val s = """ |{"@type":{"version":"1.0.2","name":"application-content","data":[]},"key-to-remove":[{"blah":"more blah"}],"@value":[]} |""".stripMargin val obj = parse(s) if (null != obj) { val obj2 = obj.removeField(_._1.equals(key)) val out = compact(render(obj2)) println(out) } } }