首先,我对Ruby非常新颖,所以对我来说很容易!我在我的sourceHash变量中解析了JSON,并试图通过“displayName”属性对数据进行分组。 JSON格式是这样的(我已经简化它在不改变结构):RUBY通过解析REST对数据进行分组JSON结果属性
{
"results": [
{
"id": "12345",
"title": "my blog post",
"history": {
"createdOn": "2017-09-18 15:38:26",
"createdBy": {
"userName": "myUserName",
"displayName": "Michael W."
}
}
},
{ ... same stuff for some other blog post ... },
{ ... same stuff for some other blog post ... },
{ ... same stuff for some other blog post ... }
]
}
基本上,有两件事情我想做的事情。 想象一下这个列表作为他们的“包括作者数据的博客文章列表”。
- 找到谁贴的最大量条目的人
- 获得前10名的博客,通过他们的博客文章数量排序,降
所以第一个会是这个样子:
迈克尔W.(51个)
然而,第二个应该是这样的:
- 迈克尔·韦恩(51个)
- 艾米莉亚·克拉克(36个)
- 查理兹·塞隆(19个)
- 斯嘉丽约翰逊(7职位)
我身边这些查询出场,试图我LINQ逻辑合并到这一点,但我失败了......(我是一个Ruby的小白就这样简单!)
sourceHash = @mainData["results"]
hashSetPrimary = sourceHash.group_by{|h| h['history']['createdBy']['displayName']}
return hashSetPrimary
所以很长的故事简而言之,我正在尝试编写单独的查询来按照这些条件对数据进行分组,任何帮助都会被赞赏,因为我找不到任何适当的方式来执行此操作。
尝试像'hashSetPrimary = hashSetPrimary.map {| K,V | [k,v.count]} .sort_by(:last)'。 – henrebotha
@henrebotha它是抱怨它无法找到计数方法'未定义的方法'计数'nil:NilClass(NoMethodError)' –