【问题标题】:Group JSON by data?按数据分组 JSON?
【发布时间】:2012-04-24 09:24:15
【问题描述】:

我有以下 JSON 数据:

{
  "events":
  {
      "event":
      [
          {
              "city":"Birmingham",
              "state":"AL",
              "country":"US",
              "lat":"33.5206608",
              "lng":"-86.80249",
              "status":"Delivered",
              "occured_at":"2012-04-06
14:17:00 UTC"
          },
          {
              "city":"Birmingham",
              "state":"AL",
              "country":"US",
              "lat":"33.5206608",
              "lng":"-86.80249",
              "status":"Out
For Delivery",
              "occured_at":"2012-04-06 04:44:00 UTC"
          },
          {
              "city":"Birmingham",
              "state":"AL",
              "country":"US",
              "lat":"33.5206608",
              "lng":"-86.80249",
              "status":"Arrival
Scan",
              "occured_at":"2012-04-05 19:07:00 UTC"
          },
          {
              "city":"Doraville",
              "state":"GA",
              "country":"US",
              "lat":"33.8981579",
              "lng":"-84.2832564",
              "status":"Departure
Scan",
              "occured_at":"2012-04-05 17:08:00 UTC"
          },
          {
              "city":"Doraville",
              "state":"GA",
              "country":"US",
              "lat":"33.8981579",
              "lng":"-84.2832564",
              "status":"Arrival
Scan",
              "occured_at":"2012-04-05 11:15:00 UTC"
          },
          {
              "city":"Spartanburg",
              "state":"SC",
              "country":"US",
              "lat":"34.9495672",
              "lng":"-81.9320482",
              "status":"Departure
Scan",
              "occured_at":"2012-04-05 08:42:00 UTC"
          },
          {
              "city":"Spartanburg",
              "state":"SC",
              "country":"US",
              "lat":"34.9495672",
              "lng":"-81.9320482",
              "status":"Arrival
Scan",
              "occured_at":"2012-04-05 08:21:00 UTC"
          },
          {
              "city":"Greensboro",
              "state":"NC",
              "country":"US",
              "lat":"36.0726354",
              "lng":"-79.7919754",
              "status":"Departure
Scan",
              "occured_at":"2012-04-05 04:45:00 UTC"
          },
          {
              "city":"Greensboro",
              "state":"NC",
              "country":"US",
              "lat":"36.0726354",
              "lng":"-79.7919754",
              "status":"Origin
Scan",
              "occured_at":"2012-04-05 00:11:00 UTC"
          },
          {
              "city":null,
              "state":null,
              "country":"US",
              "status":"Billing
Information Received",
              "occured_at":"2012-04-04 18:20:27 UTC"
          }
      ]
  }
}

我需要做的是按城市、州和国家/地区的组合对数据进行分组,但仍然返回每个项目的数据。

因此,例如,按“伯明翰,阿拉巴马州,美国”进行分组,但仍然可以使用它遍历每个事件的 status(即已交付、外送、到达扫描)。

【问题讨论】:

  • 这是 JSON 的事实真的无关紧要,对吧?您可以使用 JSON.parse 将其转换为 Ruby 哈希,然后直接使用它(如果需要,最后将其返回为 JSON)。

标签: ruby-on-rails ruby arrays json


【解决方案1】:

你想要的魔法是Enumerable#group_by:

require 'json'
all = JSON.parse(DATA.read)['events']['event']
all.group_by{ |h| [h['city'],h['state'],h['country']] }.each do |loc,events|
  puts "'#{loc.join(',')}': #{events.length} event#{:s if events.length!=1}"
  print "--> "
  puts events.map{ |e| e['status'] }.join(', ')
end

#=> 'Birmingham,AL,US': 3 events
#=> --> Delivered, Out For Delivery, Arrival Scan
#=> 'Doraville,GA,US': 2 events
#=> --> Departure Scan, Arrival Scan
#=> 'Spartanburg,SC,US': 2 events
#=> --> Departure Scan, Arrival Scan
#=> 'Greensboro,NC,US': 2 events
#=> --> Departure Scan, Origin Scan
#=> ',,US': 1 event
#=> --> Billing Information Received

请注意,在上面,loc 是由group_by 评估的块返回的三元素数组,events 是同一组中所有项目的数组。

【讨论】:

  • 正确。正是我想要的。谢谢!
【解决方案2】:

您可以使用关联数组,以便您可以使用字符串作为键并使用它来访问您希望组合在一起的记录。例如,所有伯明翰事件将通过以下方式访问:

events.event.Birmingham[i]

所有 Spartanburg 记录都位于通过以下方式访问的数组中:

events.event.Spartanburg[i]

以下是序列化对象的部分 JSON 示例:

{
"events": {
    "event": {
        "Birmingham": [
            {
                "city": "Birmingham",
                "state": "AL",
                "country": "US",
                "lat": "33.5206608",
                "lng": "-86.80249",
                "status": "Delivered",
                "occured_at": "2012-04-06 14:17:00 UTC"
            },
            {
                "city": "Birmingham",
                "state": "AL",
                "country": "US",
                "lat": "33.5206608",
                "lng": "-86.80249",
                "status": "Out For Delivery",
                "occured_at": "2012-04-06 04:44:00 UTC"
            }
        ],
        "Spartanburg": [
            {
                "city":"Spartanburg",
                "state":"SC",
                "country":"US",
                "lat":"34.9495672",
                "lng":"-81.9320482",
                "status":"Departure Scan",
                "occured_at":"2012-04-05 08:42:00 UTC"
            },
            {
                "city": "Spartanburg",
                "state": "SC",
                "country": "US",
                "lat": "34.9495672",
                "lng": "-81.9320482",
                "status": "Arrival Scan",
                "occured_at": "2012-04-05 08:21:00 UTC"
            }
        ]
    }
}
}

这是一个如何在 Ruby 中生成 JSON 的示例。示例取自JSON implementation for Ruby

puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])

以下是该命令生成的 JSON:

[
    1,
    2,
    {
      "a": 3.141
    },
    false,
    true,
    null,
    {
      "json_class": "Range",
      "data": [
         4,
        10,
        false
      ]
    }
]

这是一个部分示例,将向您展示如何从头开始使用修改后的示例:

puts JSON.pretty_generate({"events"=>{"event=>{"Birmingham"=>[{"city"=>"Birmingham","state"=>"AL"},{"city"=>"Birmingham","state"=>"AL"}]},{"Spartanburg"=>[{"city"=>"Spartanburg","state"=>"GA"}]}}})

【讨论】:

  • 这看起来像我可能需要的。如何将我当前的 JSON 代码转换为您的示例?
  • 查看stackoverflow.com/questions/5863477/…。它描述了如何在 Ruby 中构建 JSON 对象。没有看到你的代码,我不能比这更具体。如果有帮助,[] 符号表示“数组”,JSON 包含每个城市的单独数组。
  • 另外,看看这个。它有更复杂的 JSON 字符串示例:flori.github.com/json/doc/index.html
  • JSON 对象直接来自第三方 API。没有 ActiveRecord 对象或类似的东西。就像我的示例代码一样。
  • @Shpigford - 我用一个例子更新了我的答案。它尚未经过测试,但足以让您开始并演示该概念。
猜你喜欢
  • 2020-03-01
  • 2016-02-17
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 2015-02-04
相关资源
最近更新 更多