4

我有以下 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每个事件(即已交付、外送、到达扫描)。

4

2 回答 2

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

于 2012-04-12T03:25:32.643 回答
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 的示例。该示例取自Ruby 的 JSON 实现

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"}]}}})
于 2012-04-12T02:57:28.653 回答