您好我正在使用ruby-2.5.0和Rails 5处理RoR项目。我有一个嵌套的Hash如下: -
{
"lines":[
{
"Words":[
{
"WordText":"IGA",
"Left":619,
"Top":948,
"Height":20,
"Width":9
},
{
"WordText":"AU",
"Left":905,
"Top":951,
"Height":20,
"Width":18
}
],
"MaxHeight":21,
"MinTop":948
},
{
"Words":[
{
"WordText":"TAX",
"Left":629,
"Top":977,
"Height":21,
"Width":28
},
{
"WordText":"485",
"Left":895,
"Top":980,
"Height":20,
"Width":28
}
],
"MaxHeight":21,
"MinTop":977
},
{
"Words":[
{
"WordText":"IRRESISTIBLY",
"Left":700,
"Top":1103,
"Height":16,
"Width":105
}
],
"MaxHeight":16,
"MinTop":1103
},
{
"Words":[
{
"WordText":"LOW",
"Left":700,
"Top":1124,
"Height":15,
"Width":39
},
{
"WordText":"PRICES.",
"Left":745,
"Top":1124,
"Height":15,
"Width":61
}
],
"MaxHeight":15,
"MinTop":1124
},
{
"Words":[
{
"WordText":"EVERYTHING.",
"Left":701,
"Top":1147,
"Height":16,
"Width":104
}
],
"MaxHeight":16,
"MinTop":1147
},
{
"Words":[
{
"WordText":"EVERY",
"Left":711,
"Top":1169,
"Height":16,
"Width":48
},
{
"WordText":"DAY.",
"Left":764,
"Top":1169,
"Height":16,
"Width":31
}
],
"MaxHeight":16,
"MinTop":1169
},
{
"Words":[
{
"WordText":"06/02/18",
"Left":581,
"Top":1826,
"Height":19,
"Width":78
},
{
"WordText":"16:25",
"Left":671,
"Top":1826,
"Height":20,
"Width":46
}
],
"MaxHeight":20,
"MinTop":1826
},
{
"Words":[
{
"WordText":"0553",
"Left":640,
"Top":1854,
"Height":19,
"Width":38
}
],
"MaxHeight":19,
"MinTop":1854
},
{
"Words":[
{
"WordText":"CREDIT",
"Left":581,
"Top":1882,
"Height":20,
"Width":57
},
{
"WordText":"ACCOUNT",
"Left":650,
"Top":1882,
"Height":21,
"Width":67
}
],
"MaxHeight":21,
"MinTop":1882
},
{
"Words":[
{
"WordText":"APSN",
"Left":580,
"Top":1910,
"Height":21,
"Width":38
},
{
"WordText":"0000",
"Left":630,
"Top":1911,
"Height":20,
"Width":37
},
{
"WordText":"ATC",
"Left":688,
"Top":1911,
"Height":21,
"Width":29
},
{
"WordText":"0543",
"Left":728,
"Top":1912,
"Height":20,
"Width":38
}
],
"MaxHeight":21,
"MinTop":1910
},
{
"Words":[
{
"WordText":"PURCHASE",
"Left":580,
"Top":1939,
"Height":20,
"Width":77
}
],
"MaxHeight":20,
"MinTop":1939
},
{
"Words":[
{
"WordText":"(OO)APPRDVED",
"Left":837,
"Top":1970,
"Height":21,
"Width":116
}
],
"MaxHeight":21,
"MinTop":1970
},
{
"Words":[
{
"WordText":"28",
"Left":735,
"Top":2224,
"Height":20,
"Width":19
},
{
"WordText":"day",
"Left":764,
"Top":2224,
"Height":24,
"Width":29
},
{
"WordText":"returns",
"Left":804,
"Top":2226,
"Height":19,
"Width":68
}
],
"MaxHeight":24,
"MinTop":2224
},
{
"Words":[
{
"WordText":"WE",
"Left":537,
"Top":2251,
"Height":20,
"Width":19
},
{
"WordText":"WITH",
"Left":881,
"Top":2254,
"Height":19,
"Width":39
}
],
"MaxHeight":20,
"MinTop":2251
},
{
"Words":[
{
"WordText":"YOUR",
"Left":538,
"Top":2278,
"Height":21,
"Width":37
},
{
"WordText":"VISIT",
"Left":812,
"Top":2280,
"Height":20,
"Width":49
}
],
"MaxHeight":21,
"MinTop":2278
},
{
"Words":[
{
"WordText":"kvt-",
"Left":674,
"Top":2308,
"Height":20,
"Width":57
},
{
"WordText":"icy",
"Left":913,
"Top":2310,
"Height":22,
"Width":28
}
],
"MaxHeight":24,
"MinTop":2308
}
],
"HasOverlay":true,
"Message":"Total lines: 60"
}
我需要找到键'WordText'的所有值,因为您可以看到键在哈希中是常见的。有没有找到嵌套哈希中键的所有值的方法?请帮助我如何获得键'WordText'的所有值。提前谢谢。
答案 0 :(得分:3)
每个WordText键位于一个Words键中,同时位于一个称为行的内部,因此您可以映射它们。首先访问lines
密钥,然后在每个Words
上获取WordText
:
p nested[:lines].flat_map { |hash| hash[:Words].map { |inner| inner[:WordText] } }
# ["IGA", "AU", "TAX", "485", "IRRESISTIBLY", ...]