2012-04-12 112 views
4

我有以下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" 
      } 
     ] 
    } 
} 

我需要做的是團體經市,州和國家的組合數據,但仍從每個項目返回數據。

因此,例如,「伯明翰,AL,美國」組,但仍然能夠遍歷每個事件的status(即交付,送貨,到達掃描)。

+0

事實上,這是JSON是真的無關緊要,對吧?您可以使用'JSON.parse'將其轉換爲Ruby哈希,然後使用該哈希(如果需要,最終將其返回給JSON)。 – Phrogz 2012-04-12 03:30:14

回答

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是在同一組的所有項目的陣列。

+0

現貨。正是我在找什麼。謝謝! – Shpigford 2012-04-12 03:45:45

0

您可以使用關聯數組,以便您可以使用字符串作爲鍵並使用它訪問您希望組合在一起的記錄。例如,所有的伯明翰事件會被訪問:

events.event.Birmingham[i] 

和所有的斯帕坦堡的記錄被設置在陣列中通過訪問:

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

這看起來像我可能需要的。我將如何將當前的JSON代碼轉換爲您的示例? – Shpigford 2012-04-12 02:59:08

+0

退房http://stackoverflow.com/questions/5863477/how-do-i-build-a-json-object。它描述瞭如何在Ruby中構建JSON對象。沒有看到你的代碼,我不能比這更具體。如果有幫助,'[]'符號表示「數組」,JSON由每個城市的單獨數組組成。 – jmort253 2012-04-12 03:03:35

+0

另外,看看這個。它有更復雜的JSON字符串的示例:http://flori.github.com/json/doc/index.html – jmort253 2012-04-12 03:05:51