【问题标题】:How to return json date from MVC4 controller in ISO format如何以 ISO 格式从 MVC4 控制器返回 json 日期
【发布时间】:2013-11-17 20:37:43
【问题描述】:

我尝试使用来自 ASP.NET MVC4 控制器的 Json.Net 以 ISO 格式返回日期

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; }
    ...
}

但它会将整个对象的 json 表示法作为单个字符串返回。

如何将 ISO 格式的日期作为 json 对象返回?

【问题讨论】:

  • 您可以免费使用 ServiceStack 序列化程序吗?
  • 我可以在 C# 中使用任何免费软件

标签: asp.net-mvc json asp.net-mvc-4 serialization json.net


【解决方案1】:

您可以使用ServiceStack JSON serializer 来实现,但首先您必须将其集成到 ASP.NET MVC。

安装包后,在应用启动中配置DateTime序列化:

JsConfig.DateHandler = JsonDateHandler.ISO8601;

为 JSON 内容创建 ActionResult 类型:

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));
    }
}

然后您可以将这些方法添加到基本控制器:

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);
}

最后你可以像这样返回结果:

return CustomJson(saleList);

【讨论】:

  • 这看起来很复杂。也许可以编写使用此转换器的 Json() 替换?
  • @Andrus 也许,但我认为这是在 MVC 应用程序中使用 SS 序列化程序的正确方法。
【解决方案2】:

您可以在使用带有 JsonSerializerSettings 参数的 SerializeObject 重载时设置设置:

public static string SerializeObject(
    Object value,
    JsonSerializerSettings settings
)

JsonSerializerSettings 有一个名为 DateFormatHandling 的属性,用于区分 Microsoft 格式和 ISO 格式。

您还可以在 JSON.NET 中使用自定义转换器。可以使用 CustomConverter 属性应用自定义转换器。

可以在 JSON.NET 文档中找到一个示例:http://james.newtonking.com/json/help/index.html

我更喜欢第一种可能性。

【讨论】:

  • 我可以创建 json 字符串但是如何从 json 控制器返回它?使用 return Json(.. )Json() 不接受 json 字符串。
  • 如果您在 MVC 项目中使用 JSON.NET,您可以全局设置设置。见stackoverflow.com/questions/13274625/…
  • 我更新了问题。使用 Json.NET 将对象作为单个字符串返回。
猜你喜欢
  • 1970-01-01
  • 2016-12-06
  • 2012-02-29
  • 2012-12-12
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
相关资源
最近更新 更多