在Ruby中,如何仅从具有散列和数组以及散列数组的散列中检索键

时间:2013-08-30 00:18:45

标签: ruby arrays json

{
    "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 

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

3 个答案:

答案 0 :(得分: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"]

答案 1 :(得分:0)

  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

答案 2 :(得分:-2)

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, [])