从哈希中删除nil值 [英] Remove nil values from hash
问题描述
我正在寻找从哈希中删除具有 nil
值的键. article
是一个存储每篇文章的类,而 attributes
方法则将该文章存储为哈希.
预期结果:
{"articles":[{"results":[{"author":null,"title":前任酒吧经理因捕食无家可归的14岁女孩而被判入狱",摘要":< p>< img src = \" http://images.theage.com.au/2015/08/24/6790912/Thumbnail999662740gisd08image.related.thumbnail.320x214.gj68pg.png1440386418031.jpg-90x60.jpg \"width = \" 90 \"height = \" 60 \"style = \" float:left; margin:4px; border:0px \"/</p>一个被捕捞14年的男人他遇到的住在沃东加(Wodonga)街道上的小女孩已被判入狱9个月.,"图像:null,"源:null,"日期:" 2015年8月24日星期一03:20:21 +0000," guid:"< guid isPermaLink = \"false \"> gj68pg</guid>," link:" http://www.theage.com.au/victoria/former-bar-manager为在无家可归者14岁的女孩被捕入狱20150824-gj68pg.html," section:null," item_type:null," updated_date:null," created_date:null,"material_type_facet:null," abstract:null," byline:null," kicker:null}]}]}
希望从上述输出中删除空值.
def属性哈希= {作者" =>@作者,标题" =>@标题,摘要" =>@概括,图像" =>@图片,源" =>@来源,"date" =>@日期}哈希= {}计数= 0article.attributes.each做| key,value |如果值==零hash [count] = article.attributes.delete(key)计数+ = 1结尾结尾hash.to_json
结果如下:
{"0":空,"1":空,"2":空,"3":空,"4":空,"5":空,"6":空,"7:null," 8:null," 9:null," 10:null}
给定的输入是有效的JSON,并且该问题具有JSON标签,因此我想提供一种通用解决方案,以递归地消除键中的键JSON对象,无论它们出现在什么地方,无论输入的JSON是什么.
该解决方案是用一种新的面向JSON的编程语言jq编写的,但是即使您不能使用jq,该解决方案也是如此简洁和优雅,以至于可能会建议您选择语言中的一般问题的实现./p>
在这里-单线:
walk(如果type =="object",则with_entries(select(.value!= null))else.end)
这以jq 1.5版为前提(请参见 https://stedolan.github.io/jq/).
如果您使用的是较旧的jq版本,则可以轻松添加walk/1的定义.(请参见例如转换密钥的名称使用jq )
更深入地了解JSON结构I am looking to remove keys from hash that have nil
value. article
is a class storing each article, and attributes
method stores the article as hash.
Expected Result:
{"articles":[{"results":[{"author":null,"title":"Former bar manager jailed for preying on homeless 14-year-old girl","summary":"<p><img src=\"http://images.theage.com.au/2015/08/24/6790912/Thumbnail999662740gisd08image.related.thumbnail.320x214.gj68pg.png1440386418031.jpg-90x60.jpg\" width=\"90\" height=\"60\" style=\"float:left;margin:4px;border:0px\"/></p>A man who preyed on a 14-year-old girl he came across living on the streets of Wodonga has been jailed for nine months.","images":null,"source":null,"date":"Mon, 24 Aug 2015 03:20:21 +0000","guid":"<guid isPermaLink=\"false\">gj68pg</guid>","link":"http://www.theage.com.au/victoria/former-bar-manager-jailed-for-preying-on-homeless-14yearold-girl-20150824-gj68pg.html","section":null,"item_type":null,"updated_date":null,"created_date":null,"material_type_facet":null,"abstract":null,"byline":null,"kicker":null}]}]}
Looking to remove null values from the above output.
def attributes
hash = {
"author" => @author,
"title" => @title,
"summary" => @summary,
"images" => @images,
"source" => @source,
"date" => @date
}
hash = {}
count = 0
article.attributes.each do |key,value|
if value == nil
hash[count] = article.attributes.delete(key)
count += 1
end
end
hash.to_json
The result is as below:
{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}
The given input is valid JSON, and the question has the JSON tag, so I'd like to offer a generic solution to the problem of recursively eliminating keys in JSON objects, wherever they occur, and no matter what the input JSON may be.
The solution is written in a new JSON-oriented programming language, jq, but even if you cannot use jq, the solution is so brief and elegant that it may suggest an implementation to the general problem in your language of choice.
Here it is - a one-liner:
walk( if type == "object" then with_entries( select(.value != null) ) else . end)
This presupposes jq version 1.5 (see https://stedolan.github.io/jq/).
If you have an older version of jq, the definition of walk/1 can easily be added. (See e.g. Transforming the name of key deeper in the JSON structure with jq)
这篇关于从哈希中删除nil值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!