在哈希数组

时间:2015-07-21 21:12:05

标签: arrays ruby hash

我正在尝试展开嵌套哈希。我的哈希有这种结构:

    {
   "errorDescription":"",
   "message":"",
   "resultCode":"OK",
   "resultObj":{
      "categoryList":[
         {
            "asNew":"N",
            "categoryId":40000000,
            "categoryList":[
               {
                  "asNew":"N",
                  "categoryId":40000007,
                  "categoryList":[
                  ],
                  "categoryName":"CATALOGO",
                  "categoryType":"TYPE_NODE",
                  "contentId":40000007,
                  "contentTitle":"CATALOGO",
                  "contentType":"CATEGORY_NODE",
                  "orderId":0,
                  "urlPictures":""
               },
               {
                  "asNew":"N",
                  "categoryId":40000018,
                  "categoryList":[
                     {
                        "asNew":"N",
                        "categoryId":40000019,
                        "categoryList":[

                        ],
                        "categoryName":"CANALI CALCIO",
                        "categoryType":"TYPE_VOD",
                        "contentId":40000019,
                        "contentTitle":"CALCIO & SPORT",
                        "contentType":"CATEGORY_LEAF",
                        "orderId":0,
                        "urlPictures":""
                     },
                     {
                        "asNew":"N",
                        "categoryId":40000020,
                        "categoryList":[

                        ],
                        "categoryName":"CANALI CINEMA",
                        "categoryType":"TYPE_VOD",
                        "contentId":40000020,
                        "contentTitle":"CINEMA & SERIE",
                        "contentType":"CATEGORY_LEAF",
                        "orderId":1,
                        "urlPictures":""
                     }
                  ],
                  "categoryName":"CANALI TV",
                  "categoryType":"TYPE_NODE",
                  "contentId":40000018,
                  "contentTitle":"CANALI TV",
                  "contentType":"CATEGORY_NODE",
                  "orderId":1,
                  "urlPictures":""
               }
            ],
            "categoryName":"ROOT",
            "categoryType":"TYPE_NODE",
            "contentId":40000000,
            "contentTitle":"ROOT",
            "contentType":"",
            "orderId":0,
            "urlPictures":""
         }
      ]
   }
}

我必须在对象数组中转换此哈希,其中每个对象都具有以下结构:

                {
                  "asNew":"N",
                  "categoryId":40000007,
                  "categoryName":"CATALOGO",
                  "categoryType":"TYPE_NODE",
                  "contentId":40000007,
                  "contentTitle":"CATALOGO",
                  "contentType":"CATEGORY_NODE",
                  "orderId":0,
                  "urlPictures":""
               }

基本上每个键“categoryList”都有一个数组作为值,这个数组有一个或多个散列与另一个键“categoryList”。我必须将每个categoryList数组中的所有对象移动到另一个没有categoryList'skey'的数组。

修改

我将此方法添加到Hash类

class Hash
  def find_all_values_for(key)
    result = []
    result << self[key]
    unless self.values.nil?
      self.values.each do |values|
        values = [values] unless values.is_a? Array
        values.each do |value|
          result += value.find_all_values_for(key) if value.is_a? Hash
        end
      end
    end
    result.flatten.compact
  end
end

我将结果数组映射为删除CategoryList键

h.map { |c| c.except!('categoryList') }

它有效,但是有一种更有效的方法。

1 个答案:

答案 0 :(得分:0)

此代码将返回请求的对象数组。

def flatten_categoryList(categoryList)
  categoryList.inject([]) do |result, item|
    result + flatten_categoryList(item.delete("categoryList")) << item
  end
end

flatten_categoryList(h["resultObj"]["categoryList"])

我无法运行你的基准,所以判断解决方案目前是主观的。