如题,采用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.jsonimport org.json4s.DefaultFormatsimport org.json4s.JsonDSL._import org.json4s.jackson.JsonMethods._object Course2 {implicit val formats = DefaultFormatsdef 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\":[]}|\"\"\".stripMarginval obj = parse(s)if (null != obj) {val obj2 = obj.removeField(_._1.equals(key))val out = compact(render(obj2))println(out)}}}