如何以指定格式通过JSON返回日期?

时间:2011-02-11 15:38:47

标签: asp.net-mvc json

如果你进入你的SO个人资料并点击'访问过的日子',你会看到一个方便的jQuery日期选择器,你访问该网站的日子是一个甜美的绿色。我正在尝试类似的东西,并开始挑选SO的实现。我一直在弄清楚他们如何能够格式化并以他们使用的特定JSON格式返回访问日期。这是我的副本(您可以通过检查该页面的来源找到类似的内容):

var visited = {2009:{5:{28:1,29:1},6:{3:1,4:1,5:1,7:1,8:1,9:1,10 :1,11:1,12:1,13:1,14:1,15:1,16:1,17:1,19:1,20:1,21:1,22:1,23:1 ,24:1,25:1,27:1,29:1,30:1},7:{1:1,2:1,3:1,4:1,5:1,6:1,7 :1,8:1,9:1,10:1,11:1,12:1,13:1,14:1,15:1,16:1,18:1,20:1,21:1 ,22:1,23:1,24:1,26:1,27:1,28:1,29:1,30:1,31:1},8:{2:1,3:1,4 :1,5:1,6:1,7:1,11:1,12:1,13:1,17:1,20:1,24:1,25:1,27:1,28:1 },9:{9:1,10:1,15:1,23:1,24:1},10:{1:1,5:1,7:1,8:1,9:1,12 :1,13:1,20:1,21:1,22:1,27:1,28:1,30:1},11:{5:1,9:1,10:1,16:1 ,17:1,24:1,25:1,26:1,30:1},12:{2:1,7:1,8:1,9:1,11:1,12:1,13 :1,14:1,15:1,16:1,17:1,18:1,21:1,22:1,23:1,28:1,30:1}},2010:{1: {5:1,8:1,11:1,12:1,13:1,14:1,15:1,18:1,19:1,20:1,21:1,26:1,27 :1,28:1},2:{3:1,10:1,17:1,22:1,23:1,26:1},3:{1:1,2:1,3:1 ,5:1,8:1},4:{8:1,9:1,13:1,19:1,20:1,21:1,22:1,26:1,28:1,29 :1},5:{3:1,4:1,5:1,6:1,7:1,11:1,12:1,17:1,28:1},6:{2:1 ,4:1,7:1,8:1,9:1,10:1,14:1,15:1,16:1,17:1,18:1,21:1,23:1,25 :1},7:{7:1,8:1,9:1,12:1,21:1,22:1,23:1,28:1,29:1},8:{2:1 ,3:1,4:1,5:1,9:1,10:1,11:1,16:1,17:1,18:1,19:1,20:1,25:1 ,26:1,29:1,30:1,31:1},9:{1:1,8:1},10:{6:1,11:1,15:1,21:1,26 :1,27:1,28:1},11:{3:1,4:1,8:1,11:1,15:1,16:1,17:1,19:1,20:1 ,21:1,23:1,30:1},12:{1:1,2:1,13:1,14:1,15:1,16:1,17:1,20:1,22 :1,23:1,28:1}},2011:{1:{3:1,5:1,6:1,7:1,12:1,14:1,18:1,20:1 ,21:1,25:1,26:1,27:1,28:1,31:1},2:{1:1,2:1,3:1,6:1,7:1,8 :1,9:1,10:1,11:1}}};

它基本上是一个数组:[年] [月] [天]。我没有以这种格式发回数据,并且想知道是否有人(可能是其中一名SO工作人员)有任何见解。

希望没有人认为我正在用这个问题扯掉SO。我真的很好奇他们是如何做到的。

编辑:为了澄清,我问的是如果我掌握.NET DateTime对象并希望以所提及的格式返回JSON,控制器操作可能是什么样的。

我的测试控制器方法如下:

[HttpPost]
public JsonResult GetDates(int id)
{
    var model = BaseRepository.FindById(id);
    testDates = new DateTime[] { DateTime.Today, DateTime.Today.AddDays(1) };
    return Json(testDates);
}

当我通过AJAX调用它时:

$.ajax({
    type: 'POST',
    url: '<%= Url.Action("ClosedDates", "Venue", new { id = Model.Id }) %>',
    dataType: 'json',
    success: function (data) { alert(data); }
});

警报显示了预期的日期列表,但是以.NET日期格式显示。我不确定我是应该按摩控制器中的数据,还是在客户端的回调中。

编辑2

好吧,也许使用该数组有助于更好地理解这一点。再次,关闭SO的实现(注意beforeShowDay函数如何解析正在评估的当前日期的数组):

var visited = { 2009: { 5: { 28: 1, 29: 1 }, 6: { 3: 1, 4: 1, 5: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 27: 1, 29: 1, 30: 1 }, 7: { 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 18: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 26: 1, 27: 1, 28: 1, 29: 1, 30: 1, 31: 1 }, 8: { 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 11: 1, 12: 1, 13: 1, 17: 1, 20: 1, 24: 1, 25: 1, 27: 1, 28: 1 }, 9: { 9: 1, 10: 1, 15: 1, 23: 1, 24: 1 }, 10: { 1: 1, 5: 1, 7: 1, 8: 1, 9: 1, 12: 1, 13: 1, 20: 1, 21: 1, 22: 1, 27: 1, 28: 1, 30: 1 }, 11: { 5: 1, 9: 1, 10: 1, 16: 1, 17: 1, 24: 1, 25: 1, 26: 1, 30: 1 }, 12: { 2: 1, 7: 1, 8: 1, 9: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 21: 1, 22: 1, 23: 1, 28: 1, 30: 1} }, 2010: { 1: { 5: 1, 8: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 18: 1, 19: 1, 20: 1, 21: 1, 26: 1, 27: 1, 28: 1 }, 2: { 3: 1, 10: 1, 17: 1, 22: 1, 23: 1, 26: 1 }, 3: { 1: 1, 2: 1, 3: 1, 5: 1, 8: 1 }, 4: { 8: 1, 9: 1, 13: 1, 19: 1, 20: 1, 21: 1, 22: 1, 26: 1, 28: 1, 29: 1 }, 5: { 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 11: 1, 12: 1, 17: 1, 28: 1 }, 6: { 2: 1, 4: 1, 7: 1, 8: 1, 9: 1, 10: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 21: 1, 23: 1, 25: 1 }, 7: { 7: 1, 8: 1, 9: 1, 12: 1, 21: 1, 22: 1, 23: 1, 28: 1, 29: 1 }, 8: { 2: 1, 3: 1, 4: 1, 5: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 25: 1, 26: 1, 29: 1, 30: 1, 31: 1 }, 9: { 1: 1, 8: 1 }, 10: { 6: 1, 11: 1, 15: 1, 21: 1, 26: 1, 27: 1, 28: 1 }, 11: { 3: 1, 4: 1, 8: 1, 11: 1, 15: 1, 16: 1, 17: 1, 19: 1, 20: 1, 21: 1, 23: 1, 30: 1 }, 12: { 1: 1, 2: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 20: 1, 22: 1, 23: 1, 28: 1} }, 2011: { 1: { 3: 1, 5: 1, 6: 1, 7: 1, 12: 1, 14: 1, 18: 1, 20: 1, 21: 1, 25: 1, 26: 1, 27: 1, 28: 1, 31: 1 }, 2: { 1: 1, 2: 1, 3: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1}} };
        $('#calendar').datepicker({
            beforeShowDay: function (date) {
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var hasVisited = visited[y] && visited[y][m] && visited[y][m][d];
                return [false, (hasVisited ? 'ui-state-active' : ''), (y + '-' + m + '-' + d)];
            }
        });

4 个答案:

答案 0 :(得分:1)

我会创建一个模型来迎合那个电话。您将在客户端进行更多处理,但它将更多OO并且您可以使用JS点表示法来访问每个对象级别。

Ajax Call(我使用的是Razor,但你想将ID作为数据元素传递):

 $.ajax({
    type: 'POST',
    url: '@Url.Action("ClosedDates", "Venue")',
    data: {id: 1},
    dataType: 'json',
    success: function (data) { console.log({data: data});  }
});

型号:

 public class DateListModel
{
    public int Month { get; set; }
    public List<string> Days { get; set; }
}

   public class DateModel
{
    public int Year { get; set; }
    public List<DateListModel> DateList { get; set; }
}

控制器(带测试数据):

[HttpPost]
public JsonResult ClosedDates(int id)
{
       var dateList2009 = new List<DateListModel> { 
            new DateListModel{Month =5, Days = new List<string>{"28", "29"}},
            new DateListModel{Month = 6, Days = new List<string>{"3", "49", "5"}},
        };
        var dateList2010 = new List<DateListModel>{
            new DateListModel{Month = new DateTime(2010,1,1).Month, Days = new List<string>{"5", "8", "11"}},
            new DateListModel{Month = new DateTime(2010,2,1).Month, Days = new List<string>{"3", "10", "11"}},
        };

        var data = new List<DateModel>{
            new DateModel{ Year = 2009, DateList = dateList2009},
            new DateModel{ Year = 2010, DateList = dateList2010}
        };
        return Json(data);
    }

答案 1 :(得分:0)

当您使用JSON发送日期时,它将以以下格式解析:

\/Date(1297250340000)\/

您可以使用以下代码在javascript中解析日期时间:

var date = eval('new' + oData.replace(/\//g, ' '));

其中oData是您的控制器在JSON中返回的日期。

希望有所帮助

答案 2 :(得分:0)

根据我的经验,如果您不需要像Date数据类型那样处理客户端上的日期,则可以更容易地将其作为字符串发送到客户端。

所以 - 将日期数组作为DateTime转换为日期数组,作为您需要的格式的字符串,并将它们作为json发送给客户端。

答案 3 :(得分:0)

所以,我使用了@alexl指示的eval方法,并提出了以下内容(我会接受改进,因为这感觉很脏):

$(document).ready(function () {
$.ajax({
    type: 'POST',
    url: 'myurl',
    data: {id: 1},
    dataType: 'json',
    success: function (data) {
        var dates = new Array();
        // eval each of the dates coming from the server
        for(var i = 0; i < data.length; i++) {
            dates[i] = eval('new' + data[i].replace(/\//g, ' '));
        }
        $('#calendar').datepicker({
            beforeShowDay: function (date) {
                var isBooked = false;
                // look for the current datepicker date in the array
                for(var i = 0; i < dates.length; i++) {
                    if (dates[i].valueOf() == date.valueOf())
                    {
                        isBooked = true;
                        break;
                    }
                }
                return [false, (isBooked ? 'ui-state-active' : ''), (date.valueOf)];
            }
        });   
    }
});

});