json - 将 JSON 日期发送到 WCF 服务

标签 json wcf datepicker

我想将 json 对象发布到我的 WCF 服务

我唯一的问题是他的日期属性。我从 jquery datepicker 获取日期,我想在我的服务中获取它作为 c# datetime。

我的服务:

namespace Employee
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", 
                   RequestFormat = WebMessageFormat.Json,
                   ResponseFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Wrapped)]
        bool UpdateEmployee(Employee Employee);
    }
}

这是员工:
[DataContract]
public class Employee
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Department { get; set; }

    [DataMember]
    public int Salary { get; set; }

    [DataMember]
    public DateTime Hired { get; set; }
}

所有其他属性都可以正常工作。我只需要将我的日期字符串转换为 json 日期。

最佳答案

DateTime 对象的预期格式不是 jQuery 日期选择器返回的格式。 WCF 需要 ASP.NET 格式的日期(例如 \/Date(1234567890)\/ )。

不过,您可以使用其他格式,但这并不简单(至少在 .NET 4.0 之前不会;在 4.5 上这会好很多)。基本上,您将使用一个字符串属性(如果您的服务在完全信任下运行,它可以是私有(private)的),该属性将从线路获取值,然后在序列化过程中将其连接到 DateTime 属性。在 http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx 有更多关于这个技巧的信息,你可以在下面的代码中看到它。

namespace StackOverflow_11105856
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
                   RequestFormat = WebMessageFormat.Json,
                   ResponseFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Wrapped)]
        string UpdateEmployee(Employee Employee);
    }

    public class Service : IService1
    {
        public string UpdateEmployee(Employee Employee)
        {
            return string.Format("Name={0},Hired={1}", Employee.Name, Employee.Hired.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }

    [DataContract]
    public class Employee
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Department { get; set; }

        [DataMember]
        public int Salary { get; set; }

        public DateTime Hired { get; set; }

        [DataMember(Name = "Hired")]
        private string HiredForSerialization { get; set; }

        [OnSerializing]
        void OnSerializing(StreamingContext ctx)
        {
            this.HiredForSerialization = this.Hired.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
        }

        [OnDeserializing]
        void OnDeserializing(StreamingContext ctx)
        {
            this.HiredForSerialization = "1900-01-01";
        }

        [OnDeserialized]
        void OnDeserialized(StreamingContext ctx)
        {
            this.Hired = DateTime.ParseExact(this.HiredForSerialization, "MM/dd/yyyy", CultureInfo.InvariantCulture);
        }
    }
}

和 jQuery 调用:
    function StackOverflow_11105856_Test() {
        var url = "/StackOverflow_11105856.svc/UpdateEmployee";
        var data = {
            Name: "John Doe",
            Department: "Accounting",
            Salary: 50000,
            Hired: $("#StackOverflow_11105856_datepicker").val()
        };
        $.ajax({
            type: 'POST',
            url: url,
            contentType: "application/json",
            data: JSON.stringify({ Employee: data }),
            success: function (result) {
                $("#result").text(result.UpdateEmployeeResult);
            }
        });
    }

关于json - 将 JSON 日期发送到 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105856/

相关文章:

javascript - 使用下划线在 javascript 中重新排序对象

c++ - QJsonDocument 在 C++ 中列出或排列

c# - EntityFramework.dll 中出现“System.Data.Entity.Infrastructure.DbUpdateException”

asp.net - .SVC 文件生成的 Javascript 代理 URL 在 SSL 下不起作用

jquery - 如何从循环 div 的 Jquery Mobile Datebox 输入字段获取日期

jQuery 日期选择器,其中文本输入是只读的

javascript - 如何更改 JSON 中 localStorage 中键的值?

java - 使用 jackson 库反序列化 MongoDB $oid

c# - Linq-to-SQL 和 WCF 服务 - 数据传输对象

javascript - 如何通过 Bootstrap 日期时间选择器使用今天和明天按钮?