汇总Ruby中JSON中特定键的所有值

时间:2018-11-06 17:32:03

标签: ruby-on-rails ruby

我在数据库的列中存储了以下json。我正在使用此JSON在我的一个视图中填充表格,效果很好。我现在要做的是计算特定键的所有值的总和->每个“ {{1}”的每个(“ ownership_percentage”)的总和}”。

shareholder

只需添加另一层-owner_percentage是一个字符串,因此在执行总和之前需要将其转换为十进制/浮点数,这样一个衬里会有点长:)

我想要实现的是以下(伪代码),但我很难找到一种对我来说效果很好的衬垫。很想在这里朝正确的方向前进!

{
   "captable":{
      "id":28,
      "version":1,
      "name":"CapTable",
      "company_id":22,
      "created_at":"2018-10-31T18:56:03.965Z",
      "updated_at":"2018-10-31T18:57:30.626Z",
      "total_stocks_in_company":"400.0"
   },
   "event":{
      "status_locked":null,
      "id":51,
      "price_per_share":"1000.0",
      "total_company_stocks_after_event":"400.0",
      "name":"2nd event ",
      "date":"2018-10-31",
      "currency":"SEK",
      "valuation":"400000.0",
      "created_at":"2018-10-31T18:57:17.282Z",
      "updated_at":"2018-10-31T18:57:30.676Z",
      "captable_id":28,
      "company_id":22,
      "snapshot":"{\"captable\":{\"id\":28,\"total_stocks_in_company\":\"400.0\",\"version\":1,\"name\":\"CapTable\",\"company_id\":22,\"created_at\":\"2018-10-31T18:56:03.965Z\",\"updated_at\":\"2018-10-31T18:57:30.626Z\"},\"event\":{\"status_locked\":null,\"id\":51,\"price_per_share\":\"1000.0\",\"total_company_stocks_after_event\":\"400.0\",\"name\":\"2nd event \",\"date\":\"2018-10-31\",\"currency\":\"SEK\",\"valuation\":\"400000.0\",\"created_at\":\"2018-10-31T18:57:17.282Z\",\"updated_at\":\"2018-10-31T18:57:30.665Z\",\"captable_id\":28,\"company_id\":22,\"snapshot\":null},\"shareholders\":[{\"id\":52,\"shareholder\":\"Peter\",\"name\":\"Peter\",\"number_of_stocks\":\"100.0\",\"company_id\":22,\"created_at\":\"2018-10-31T18:55:42.730Z\",\"updated_at\":\"2018-10-31T18:57:30.637Z\",\"ownership_percentage\":\"0.25\",\"email\":\"\",\"telephone\":\"\"},{\"id\":53,\"shareholder\":\"Jane\",\"name\":\"Jane\",\"number_of_stocks\":\"100.0\",\"company_id\":22,\"created_at\":\"2018-10-31T18:55:49.490Z\",\"updated_at\":\"2018-10-31T18:57:30.644Z\",\"ownership_percentage\":\"0.25\",\"email\":\"\",\"telephone\":\"\"},{\"id\":54,\"shareholder\":\"Sally\",\"name\":\"Sally \",\"number_of_stocks\":\"200.0\",\"company_id\":22,\"created_at\":\"2018-10-31T18:55:56.192Z\",\"updated_at\":\"2018-10-31T18:57:30.651Z\",\"ownership_percentage\":\"0.5\",\"email\":\"\",\"telephone\":\"\"}]}"
   },
   "shareholders":[
      {
         "id":52,
         "shareholder":"Peter",
         "name":"Peter",
         "number_of_stocks":"50.0",
         "company_id":22,
         "created_at":"2018-10-31T18:55:42.730Z",
         "updated_at":"2018-10-31T18:58:31.406Z",
         "ownership_percentage":"0.125",
         "email":"",
         "telephone":""
      },
      {
         "id":53,
         "shareholder":"Jane",
         "name":"Jane",
         "number_of_stocks":"150.0",
         "company_id":22,
         "created_at":"2018-10-31T18:55:49.490Z",
         "updated_at":"2018-10-31T18:58:31.410Z",
         "ownership_percentage":"0.375",
         "email":"",
         "telephone":""
      },
      {
         "id":54,
         "shareholder":"Sally",
         "name":"Sally ",
         "number_of_stocks":"200.0",
         "company_id":22,
         "created_at":"2018-10-31T18:55:56.192Z",
         "updated_at":"2018-10-31T18:57:30.651Z",
         "ownership_percentage":"0.5",
         "email":"",
         "telephone":""
      }
   ]
}

注意:此示例中的owner_percentage最多应汇总为1.0(即100%)。

1 个答案:

答案 0 :(得分:1)

您必须将一个块传递到shareholders数组上的Array#sum

snapshot_json["shareholders"].sum { |x| x["ownership_percentage"].to_f }