2011-11-27 18 views
1

我有一个JSON提要,我正在解析。下面是下面的代码示例:Rails中的数组逻辑(通过JSON)

JSON

[ 
    { 
     "global_event":{ 
     "ending_at":"2011-11-07T02:00:00Z", 
     "short_url":"http://bit.ly/reAhRw", 
     "created_at":"2011-10-04T14:25:41Z", 
     "event_responses":[ 

     ], 
     "addresses":{ 
      "location":{ 
       "city":"blah", 
       "latitude":30.205288, 
       "zipcode":"343434", 
       "street":"blah", 
       "longitude":-95.475289, 
       "state":"TX" 
      } 
     }, 
     "body":"blahblahblah", 
     "euid":"2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a", 
     "title":"Fusion", 
     "updated_at":"2011-10-04T14:26:57Z", 
     "event_roles":[ 

     ], 
     "user":{ 
      "long_name":"Fusion Single", 
      "nickname":"" 
     }, 
     "event_items":[ 

     ], 
     "starting_at":"2011-11-07T00:00:00Z" 
     } 
    } 
] 

控制器

def events 
    @json = ActiveSupport::JSON.decode(open('jsonfeed').read) 
end 

查看

<ul class="events"> 
    <% @json.each do |event| %> 
     <% event.each do |e, d| %> 
      <li> 
       <h4><%= d['starting_at'].to_date.strftime('%A') %></h4> 
        <small><%= d['starting_at'].to_date.strftime('%b %d') %></small> 
        <p> 
         <%= link_to d['title'], d['short_url'] %> <span style="font-size: 12px; padding: 0 10px; font-weight: bold;">|</span> 
         <%= d['starting_at'].to_date.strftime('%I:%M %p') %> 
        </p> 
      </li> 
     <% end %> 
    <% end %> 
</ul> 

Finall Ÿ,所有这些都已到位,它的工作完美无缺。下面是我的输出如下:enter image description here

所以,最后我的问题:正如你在截图中看到,这两个项目被打印到屏幕上具有相同的日期(1月14日)。我想要做的是将两者结合起来。因此,我最终的输出结果将具有“星期六”,其下面列出了两个事件。谢谢您的帮助!

+0

你有没有试过这个:http://stackoverflow.com/questions/3884829/grouping-a-ruby-array –

回答

1

下面的代码应该按日期帮助你将你的活动:

控制器

def events 
    @json = ActiveSupport::JSON.decode(open('jsonfeed').read).group_by {|e| e["global_event"]["starting_at"].to_date.strftime('%Y-%m-%d') } 
end 

产生的数据结构:

{"2011-11-07"=> 
    [{"global_event"=> 
    { 
     "ending_at"=>"2011-11-07T02:00:00Z", 
     "short_url"=>"http://bit.ly/reAhRw", 
     "created_at"=>"2011-10-04T14:25:41Z", 
     "event_responses"=>[], 
     "addresses"=> 
     {"location"=> 
     {"city"=>"blah", 
      "latitude"=>30.205288, 
      "zipcode"=>"343434", 
      "street"=>"blah", 
      "longitude"=>-95.475289, 
      "state"=>"TX" 
     } 
     }, 
     "body"=>"blahblahblah", 
     "euid"=>"2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a", 
     "title"=>"Fusion", 
     "updated_at"=>"2011-10-04T14:26:57Z", 
     "event_roles"=>[], 
     "user"=>{"long_name"=>"Fusion Single", "nickname"=>""}, 
     "event_items"=>[], 
     "starting_at"=>"2011-11-07T00:00:00Z" 
    } 
    }] 
} 

查看

<ul class="events"> 
    <% @json.each do |date, events| %> 
     <li> 
      <h4><%= date.to_date.strftime('%A') %></h4> 
      <small><%= date.to_date.strftime('%b %d') %></small> 
       <% events.each do |e| %> 
        <p> 
         <%= link_to e['global_event']['title'], e['global_event']['short_url'] %> <span style="font-size: 12px; padding: 0 10px; font-weight: bold;">|</span> 
         <%= e['global_event']['starting_at'].to_date.strftime('%I:%M %p') %> 
        </p> 
       <% end %> 
     </li> 
    <% end %> 
</ul> 
+0

谢谢你的代码,但它实际上打印出所有的东西,就像我的代码打印出来一样Oo – John

+0

@johnernaut它不应该是这样,你可以添加更多的事件到你的JSON事件?我可能在那里做了一个错误的假设。 –

+0

嗨Benoit,我回到这个项目后,在其他事情上工作了一段时间,发现你的答案其实是正确的。非常感谢你的帮助! – John