2013-08-30 38 views
-2
{ 
    "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 
       } 
      ] 
     } 
    ] 
} 

上面的值都是不同的,我真的不关心这些值。在Ruby中如何检索只有散列和散列数组的散列的密钥

所以我从上面需要的是

[CourseID,当然,代号,名称,资质,纪律,模块,ID,代码,名称,核心,单位,身份证,编号,名称,核心,my_key, ID,代码,名称,核心,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 

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

+1

你既没有有效的Ruby数据结构也不是有效的JSON字符串。 –

+0

@CodeGnome - 我有一些格式问题。我只是重新格式化它。 – user1126946

回答

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"] 
+0

不错的解决方案!非常紧凑。我必须记住!虽然结果数组似乎不符合OP所需的顺序,但是? – SteveTurczyn

+0

@SteveTurczyn真..我修好了。 –

+0

@GuilhermeBernal - 真棒。这对我来说非常合适。你能给我一点关于'o'实际上接收json的解释吗? – user1126946

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

这比问题更难以阅读。 – naomik