How to return json date from MVC4 controller in ISO format

谁说胖子不能爱 提交于 2019-12-08 08:32:13

问题


I tried tro return date in ISO format using Json.Net from ASP.NET MVC4 controller

public JsonResult Sales() {
  var saleList = new List<Sale>();

  ... 
        var str = JsonConvert.SerializeObject(saleList);
        return Json(str, JsonRequestBehavior.AllowGet);
        }

public class Sale
{
    public DateTime saledate { get; set; }
    ...
}

But it returns whole object json notation as single string.

How to return date in ISO format as json object ?


回答1:


You can do it with ServiceStack JSON serializer but first you have to integrate it to ASP.NET MVC.

After installing the package, configure DateTime serialization in application start:

JsConfig.DateHandler = JsonDateHandler.ISO8601;

Create an ActionResult type for JSON content:

public class CustomJsonResult : ActionResult
{
    private readonly object _data;
    private readonly string _content;
    private readonly Encoding _encoding;

    public CustomJsonResult(object data) : this(data, null, null) { }

    public CustomJsonResult(object data, string content) : this(data, content, null) { }

    public CustomJsonResult(object data, Encoding encoding) : this(data, null, encoding) { }

    public CustomJsonResult(object data, string content, Encoding encoding)
    {
        _data = data;
        _content = content;
        _encoding = encoding;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = string.IsNullOrEmpty(_content) ? "application/json" : _content;

        if (_encoding != null)
        {
            response.ContentEncoding = _encoding;
        }

        response.Write(JsonSerializer.SerializeToString(_data));
    }
}

Then you can add these methods to a base controller:

protected CustomJsonResult CustomJson(object data)
{
    return new CustomJsonResult(data);
}

protected CustomJsonResult CustomJson(object data, string content)
{
    return new CustomJsonResult(data, content);
}

protected CustomJsonResult CustomJson(object data, Encoding encoding)
{
    return new CustomJsonResult(data, encoding);
}

protected CustomJsonResult CustomJson(object data, string content, Encoding encoding)
{
    return new CustomJsonResult(data, content, encoding);
}

At last you can return the result like this:

return CustomJson(saleList);



回答2:


You can set settings when using an overload to SerializeObject that takes an JsonSerializerSettings parameter:

public static string SerializeObject(
    Object value,
    JsonSerializerSettings settings
)

The JsonSerializerSettings have a property called DateFormatHandlingused to distinguish between Microsoft Format and ISO format.

You could also use a custom converter in JSON.NET. Custom Converters can be applied using the CustomConverter attribute.

An example can be found in the JSON.NET documentation: http://james.newtonking.com/json/help/index.html

I would prefer the first possibility.



来源:https://stackoverflow.com/questions/20035870/how-to-return-json-date-from-mvc4-controller-in-iso-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!