gpt4 book ai didi

c# - Rest Sharp 没有序列化日期

转载 作者:行者123 更新时间:2023-11-30 20:46:28 24 4
gpt4 key购买 nike

当然,最新的 Restsharp 中的默认值应该能够序列化“日期时间”。

下面是我的请求构建

var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
//request.JsonSerializer = new MyJsonSerializer(); // When applying JSON.Net serialisation
request.Resource = "SaveBooking";
request.AddHeader("Content-Type", "application/json");
request.AddBody(booking);

return Execute<bool>(request);

我已经尝试实现 JSON.NET 序列化程序并与 restsharp 集成 as suggested in this question ,链接到他们的笔记。但我有同样的问题。

它正在构建的 JSON 如下:

 "BookingDate": "2014-11-13T09:31:02.0667967+00:00",
"StartTime": "2014-11-13T10:30:00",
"EndTime": "2014-11-13T17:00:00",

在尝试反序列化时,我得到的错误是下面的形式。

DateTime content '2014-11-12T16:20:30.4635576Z' does not start with '\/Date(' and end with ')\/' as required for JSON.'.

很明显它不是序列化日期。但我不知道为什么。或者如何修复它。

我正在反序列化其余端点

     [OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveBooking")]
Boolean SaveBooking(RoomBookingEntry Booking);

带参数

  public class RoomBookingEntry
{
public DateTime BookingDate { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}

最佳答案

您可以像这样告诉 JSON.Net 在序列化日期时使用正确的日期:

var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
JsonConvert.SerializeObject(obj, settings);

还有另一个默认执行此操作的 JSON 序列化库:https://www.nuget.org/packages/ServiceStack.Text/

var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.Resource = "SaveBooking";

var json = JsonSerializer.SerializeToString(booking);
request.AddParameter("application/json", json, ParameterType.RequestBody);

关于c# - Rest Sharp 没有序列化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26892366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com