1

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 ?

Andrus
  • 26,339
  • 60
  • 204
  • 378

2 Answers2

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);
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
0

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.

Sascha
  • 10,231
  • 4
  • 41
  • 65
  • I can create json string but how to return it from json controller ? Uing `return Json(.. )` Json() does not accept json string. – Andrus Nov 17 '13 at 20:58
  • If you use JSON.NET in your MVC project you can set the settings globally. See http://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api – Sascha Nov 17 '13 at 21:03
  • I updated question. using Json.NET returns object as single string. – Andrus Nov 17 '13 at 21:13