按数据分组JSON?

时间:2012-04-12 02:37:18

标签: ruby-on-rails ruby arrays json

我有以下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(即交付,交付交付,到达扫描)。 / p>

2 个答案:

答案 0 :(得分:4)

你想要的魔法是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是同一组中所有项目的数组。

答案 1 :(得分:0)

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

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"}]}}})