1

我有 JSON 格式的数据。我想像(d/m/y)这样格式化日期,但我有这种格式的记录 > "event_group_end":"2069-12-31T00:00:00Z",

请问怎么做?

Data=    [
              {
              "country_group":null,
              "country_id":null,
              "created_at":"2013-07-02T14:43:28Z",
              "event_country":"aut",
              "event_group_city":"Innsbruck",
              "event_group_end":"2069-12-31T00:00:00Z",
              "event_group_start":"2069-12-31T00:00:00Z",
              "event_group_title":"asdfasfsadf",
              "event_location_id":null,
              "id":11975,
              "iso":null,
              "status":0,
              "updated_at":"2013-07-25T10:41:26Z",
              "venue_id":"1027"
           },
           {
              "country_group":null,
              "country_id":null,
              "created_at":"2013-07-02T14:43:26Z",
              "event_country":"eng",
              "event_group_city":"London",
              "event_group_end":"2013-03-22T00:00:00Z",
              "event_group_start":"2013-03-22T00:00:00Z",
              "event_group_title":"jipj",
              "event_location_id":null,
              "id":11915,
              "iso":null,
              "status":0,
              "updated_at":"2013-07-25T13:27:54Z",
              "venue_id":"1264"
           },

            ]
4

2 回答 2

2

您可以在模型中编写访问器方法以获取格式化日期并在定义的键中获取格式化日期

class EventModel < ActiveRecord::Base

    #model code
    def start_date_display
        self[:event_group_start].strftime("%m/%d/%Y")
    end

    def end_date_display
        self[:event_group_end].strftime("%m/%d/%Y")
    end

end

现在当你调用 json 方法时这样做

@data.to_json :methods => [:start_date_display,:end_date_display]
于 2013-07-26T12:14:55.170 回答
0
Data[1].each do |d|
   d["event_group_end"].to_date.strftime("%d/%m/%Y")
end
于 2013-07-26T12:54:12.860 回答