scala json4s刪除json裏面的key

 

如題,採用json4s,scala刪除json裏面的key?json

好比有這麼一段json:{"@type":{"version":"1.0.2","name":"application-content","data":[]},"key-to-remove":[{"blah":"more blah"}],"@value":[]},要刪除key-to-remove這個key,採用removeField便可。上代碼:app

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)
    }
  }
}
相關文章
相關標籤/搜索