Ruby on rails Rails中的数组逻辑(通过JSON)
我正在解析一个JSON提要。以下是以下代码示例: JSONRuby on rails Rails中的数组逻辑(通过JSON),ruby-on-rails,arrays,ruby-on-rails-3,json,Ruby On Rails,Arrays,Ruby On Rails 3,Json,我正在解析一个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":[ ], "ad
[
{
"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
def events
@json = ActiveSupport::JSON.decode(open('jsonfeed').read).group_by {|e| e["global_event"]["starting_at"].to_date.strftime('%Y-%m-%d') }
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>
<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>
-
|
最后,所有这些都到位了,它工作得非常完美。我的输出如下所示:
所以,最后我的问题是:正如您在屏幕截图中看到的,屏幕上打印的两个项目的日期相同(1月14日)。我想做的是将两者结合起来。因此,我的最终输出将是“星期六”,下面列出了两个事件。谢谢你的帮助 以下代码将帮助您按日期对事件进行分组: 控制器
def events
@json = ActiveSupport::JSON.decode(open('jsonfeed').read)
end
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 |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>
<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>
-
|
以下代码将帮助您按日期对事件进行分组:
控制器
def events
@json = ActiveSupport::JSON.decode(open('jsonfeed').read)
end
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 |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>
<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>
-
|
您尝试过这个吗:您尝试过这个吗:谢谢您的代码,但它实际上打印出了所有内容,就像我的代码打印出来一样。o@johnernaut不应该是这样,您可以向JSON事件添加更多事件吗?我可能在那里做了一个错误的假设。嗨,贝诺特,我在做了一段时间其他事情后回到这个项目,发现你的答案实际上是正确的。非常感谢你的帮助!谢谢你的代码,但它实际上打印出了所有东西,就像我的代码打印出来一样。o@johnernaut不应该是这样,您可以向JSON事件添加更多事件吗?我可能在那里做了一个错误的假设。嗨,贝诺特,我在做了一段时间其他事情后回到这个项目,发现你的答案实际上是正确的。非常感谢你的帮助!