AI智能
改变未来

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.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)}}}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » scala json4s删除json里面的key