【问题标题】:JSON Date field does not bound to C# datetime fieldJSON 日期字段不绑定到 C# 日期时间字段
【发布时间】:2019-11-04 03:21:47
【问题描述】:

我在业务实体 c# 中有以下 DateTime 字段:

public DateTime BirthDate { get; set; }

当我调用将实体对象作为 JSON 传递的 API 时收到以下消息:

DateTime content '2009-02-15T00:00:00Z' does not start with '\/Date(' and end with ')\/' as required for JSON.

我在网上搜索了很多格式,但都不适合我!

错误信息很清楚!但我有一个多小时试图向我的 API 发送请求。 请不要对我的帖子投反对票!我尽力了。

这是我通过邮递员发送的 JSON 对象:

{
  "patient": {
"Number": 20012,
"FirstName": "ِAnas",
"LastName": "Tina",
"BirthDate":"1986-12-29",
"Phone": "000000",
"Mobile": "00000",
"Address": "Damas",
"Job": "Developer",
"Note": "This is a note",
"GenderId": 1
  }
}

[DataContract]    
public class Patient
{
    [Description("Patient's Id in ECMS database")]
    [DataMember(Order = 1)]
    public int Id { get; set; }

    [Description("Patient's unique number")]
    [DataMember(Order = 2)]
    public int Number { get; set; }

    [Description("Patient's first name")]
    [DataMember(Order = 3)]
    public string FirstName { get; set; }

    [Description("Patient's last name")]
    [DataMember(Order = 4)]
    public string LastName { get; set; }

    [Description("Patient's birth date")]
    [DataMember(Order = 5)]
    public DateTime BirthDate { get; set; }
}

【问题讨论】:

  • 能否提供生成JSON的代码?
  • 尝试作为时间戳 /Date(1379048144000)/
  • @KristjanKica 仍然不起作用
  • 看起来在服务器端您正在使用 JavaScriptSerializer,但您应该使用 JsonSerializer。服务器端有什么样的应用程序? ASP.NET、WCF 还是其他?
  • 我的服务器端的 WCF。

标签: c# postman


【解决方案1】:

使用这个扩展

public class DateTimeConverter : IsoDateTimeConverter
{
    public DateTimeConverter()
    {
        base.DateTimeFormat = "yyyy-MM-dd";
    }
}

并添加到您的模型中

[JsonConverter(typeof(DateTimeConverter))]
public DateTime BirthDate { get; set; }

这样您可以以“yyyy-MM-dd”格式发送日期。如果您愿意,可以将其更改为其他格式。

【讨论】:

  • 如果不是 JsonSerializer 用于反序列化请求数据,将无效。对于 JsonSerializer 给定的数据格式是标准的,因此不需要显式转换器。
  • 我使用了提供的代码,但仍然遇到同样的异常。
【解决方案2】:

我使用相同的 json 并在一个命名空间中创建了两个类:

public partial class JsonModel
{
    [JsonProperty("patient")]
    public Patient Patient { get; set; }
}

public partial class Patient
{

    [JsonProperty("Number")]
    public string Number;

    [JsonProperty("FirstName")]
    public string FirstName;

    [JsonProperty("LastName")]
    public string LastName;

    [JsonProperty("BirthDate")]
    public DateTime BirthDate;

    [JsonProperty("Phone")]
    public string Phone;

    [JsonProperty("Mobile")]
    public string Mobile;

    [JsonProperty("Address")]
    public string Address;

    [JsonProperty("Job")]
    public string Job;

    [JsonProperty("Note")]
    public string Note;

    [JsonProperty("GenderId")]
    public int GenderId;

    // Return a textual representation of the order.
    public override string ToString()
    {
        return "FirstName: " + FirstName +
        "\nLastName: " + LastName +
         "\nBirthDate: " + BirthDate;
    }
}

为测试目的创建了一个控制台应用程序。

static void Main(string[] args)
{
    string json = @"{
     ""patient"": {
     ""Number"": 20012,
     ""FirstName"":  ""Anas"",
     ""LastName"":  ""Tina"",
     ""BirthDate"": ""1986-12-29"",
     ""Phone"":  ""000000"",
     ""Mobile"":  ""00000"",
     ""Address"":  ""Damas"",
     ""Job"":  ""Developer"",
     ""Note"":  ""This is a note"",
     ""GenderId"": 1
      }
    } ";

    var test = FromJson(json);
    Console.WriteLine(test.Patient);
    Console.ReadKey();
}

public static JsonModel FromJson(string json)
{
    // Return the result.enter code here
    return JsonConvert.DeserializeObject<JsonModel>(json);
}

最后的结果是:

FirstName: Anas
LastName: Tina
BirthDate: 12/29/1986 12:00:00 AM

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2012-02-27
相关资源
最近更新 更多