【问题标题】:In Ruby how to retrieve only the keys from a hash that has both hashes and arrays and array of hashes在 Ruby 中,如何仅从具有散列和数组以及散列数组的散列中检索键
【发布时间】:2013-08-30 00:18:45
【问题描述】:
{
    "CourseID": 1111,
    "Course": {
        "Code": "ABCD",
        "Name": "ABCD",
        "Qualification": "ABCD",
        "Discipline": "ABCD"
    },
    "Modules": [
        {
            "ID": 12345,
            "Code": "ABCD",
            "Name": "ABCD",
            "Core": true,
            "Units": [
                {
                    "ID": 23456,
                    "Code": "ABCD",
                    "Name": "ABCD",
                    "Core": true,
                    "my_key": true
                },
                {
                    "ID": 34567,
                    "Code": "ABCD",
                    "Name": "ABCD",
                    "Core": true,
                    "my_key": true
                }
            ]
        }
    ]
}

上面的值都是不同的,我并不关心这些值。

所以我需要的是

[CourseID, Course, Code, Name, Qualification, Discipline, Modules, ID, Code, Name, Core, Units, ID, Code, Name, Core, my_key, ID, Code, Name, Core, my_key]

上面的数组有重复,我想要那个。

我已经用它打破了几个小时,但就是无法理解。

something.each do |key, value|
  hash = {key => value}
  hash.map { |k, v|
    if v.is_a?(Hash)
      v.map { |x, y|
        hash = x
      }
    elsif v.is_a?(Array)
      v.map { |x, y|
        x.select { |k, v|
          hash1 = [k].include? k
        }
      }
    end
  }
end 

如果有人可以帮助我,那将非常感激。

【问题讨论】:

  • 您既没有有效的 Ruby 数据结构,也没有有效的 JSON 字符串。
  • @CodeGnome - 我遇到了一些格式问题。我刚刚对其进行了重新格式化。

标签: ruby arrays json


【解决方案1】:
require 'json'

# Decode a JSON string into a Ruby object
h = JSON.parse <<END
  # Your JSON structure here
END

# Will return all hash keys of o
def keys(o)
  # If it is an array, returns the keys of each element
  return o.map {|e| keys(e) }.flatten(1) if Array === o
  # If it is an hash, returns the keys and the keys of each value
  return o.map {|k, v| [k, *keys(v)] }.flatten(1) if Hash === o
  # Otherwise, it has no keys. Return an empty array
  []
end

keys(h) # Calls the method just defined
# => ["CourseID", "Course", "Code", "Name", "Qualification", "Discipline",
#     "Modules", "ID", "Code", "Name", "Core", "Units", "ID", "Code", "Name",
#     "Core", "my_key", "ID", "Code", "Name", "Core", "my_key"]

【讨论】:

  • 不错的解决方案!非常紧凑。我必须记住这一点!但是,结果数组似乎不是 OP 所需的顺序?
  • @SteveTurczyn true.. 我修好了。
  • @GuilhermeBernal - 太棒了。这对我来说非常有效。你能给我解释一下'o'实际上是如何接收json的吗?
  • 什么是keys方法..?
  • 我不明白你的问题,但我在答案中添加了一些解释。现在干净了吗?
【解决方案2】:
  x = {
    CourseID: 1111,
    Course: {
        Code: "ABCD",
        Name: "ABCD",
        Qualification: "ABCD",
        Discipline: "ABCD"
    },
    Modules: [
        {
            ID: 12345,
            Code: "ABCD",
            Name: "ABCD",
            Core: true,
            Units: [
                {
                    ID: 23456,
                    Code: "ABCD",
                    Name: "ABCD",
                    Core: true,
                    my_key: true
                },
                {
                    ID: 34567,
                    Code: "ABCD",
                    Name: "ABCD",
                    Core: true,
                    my_key: true
                }
            ]
        }
    ]
}

def keys_only(data)
  array = []
  keys_only_iterate(data,array)
  array
end

def keys_only_iterate(data,array)
  return unless data.is_a?(Hash) || data.is_a?(Array)
  if data.is_a?(Hash)
    data.each do |k, v|
      array << k
      keys_only_iterate(v,array)
    end
  else
    data.each do |v|
      keys_only_iterate(v,array)
    end
  end
end

array = keys_only(x)
puts array

【讨论】:

    【解决方案3】:
    def array_traverse(array, hash_keys)                                            
      2 array.each do |i|                                                               
      3 if i.class == Array                                                             
      4 hash_keys = array_traverse(i, hash_keys)                                        
      5 elsif i.class == Hash                                                           
      6 hash_keys = hash_traverse(i, hash_keys)                                         
      7 end                                                                             
      8 end                                                                             
      9 return hash_keys                                                                
     10 end                                                                             
     11 def hash_traverse(hash, hash_keys)                                              
     12 hash.keys().each do |i|                                                         
     13 if hash[i].class == Array                                                       
     14 hash_keys = array_traverse(hash[i], hash_keys)                                  
     15 elsif hash[i].class == Hash                                                     
     16 hash_keys = hash_traverse(hash[i], hash_keys)                                   
     17 end                                                                             
     18 hash_keys.push(i)                                                               
     19 end                                                                             
     20 return hash_keys                                                                
     21 end  
    a={                                                                             
     24 "CourseID"=> 1111,                                                              
     25 "Course"=> {                                                                    
     26 "Code"=> "ABCD",                                                                
     27 "Name"=> "ABCD",                                                                
     28 "Qualification"=> "ABCD",                                                       
     29 "Discipline"=> "ABCD"                                                           
     30 },                                                                              
     31 "Modules"=> [                                                                   
     32 {                                                                               
     33 "ID"=> 12345,                                                                   
     34 "Code"=> "ABCD",                                                                
     35 "Name"=> "ABCD",                                                                
     36 "Core"=> true,                                                                  
     37 "Units"=> [                                                                     
     38 {                                                                               
     39 "ID"=> 23456,                                                                   
     40 "Code"=> "ABCD",                                                                
     41 "Name"=> "ABCD",                                                                
     42 "Core"=> true,                                                                  
     43 "my_key"=> true                                                                 
     44 },                                                                              
     45 {                                                                               
     46 "ID"=> 34567,                                                                   
     47 "Code"=> "ABCD",                                                                
     48 "Name"=> "ABCD",                                                                
     49 "Core"=> true,                                                                  
     50 "my_key"=> true                                                                 
     51 }                                                                               
     52 ]                                                                               
     53 }                                                                               
     54 ]                                                                               
     55 }                                                                                
     22 puts hash_traverse(a, [])          
    

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 2011-07-24
      • 1970-01-01
      • 2012-07-20
      • 2012-08-03
      • 2011-05-01
      • 2018-12-28
      • 2023-04-10
      • 2015-04-16
      相关资源
      最近更新 更多