简体   繁体   English

如何从Argonaut Json对象中删除具有空值的键

[英]How to remove keys with null values from Argonaut Json objects

How can an extension method be written to remove all keys with a null value from an Argonaut Json object: 如何编写扩展方法以从Argonaut Json对象中删除所有具有null值的键:

I tried this: 我尝试了这个:

package object Extensions {
  implicit class JsonExtensions(val json: Json) extends AnyVal {
    def removeNulls: Json = {
      json.withObject(j => JsonObject.from (j.toMap.filter(!_._2.isNull).toList))
    }
  }
}

but it only seems to be removing keys with null values at the top level of the Json object... 但是它似乎只是在Json对象的顶级删除具有null值的键...

Argonaut supports this internally, you can use PrettyParams to do what you're after: Argonaut在内部支持此功能,您可以使用PrettyParams完成以下操作:

def removeNulls: Json = {
  json.pretty(PrettyParams.nospace.copy(dropNullKeys = true)).toJson
}

Hope this covers your question! 希望这能解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM