无法使用DataContract Name属性将JSON POST请求反序列化为WCF

时间:2019-07-06 19:35:53

标签: c# json post serialization datacontract

我有一个WCF服务,它从客户端接收JSON格式的POST请求。在我的服务中,我有一个DataContract'ed类,该类应反序列化我的请求。类文件名与JSON对象中的名称不同。因此,我使用Name属性来定义JSON对象字段和WCR类字段之间的绑定。但这并不会反序列化这种方式。仅当我在JSON对象和WCF类中使用相同的文件名时,我才能正确获取请求数据。

    [DataContract]
    public class ModifyBatchRequest
    {
        [DataMember(Name = "type")]
        public int TypeID { get; set; }

        [DataMember(Name = "off", IsRequired = false, EmitDefaultValue = false)]
        public int Offers { get; set; }

        [DataMember(Name = "created", IsRequired = false, EmitDefaultValue = false)]
        public RuleData[] NewEntries { get; set; }

        [DataMember(Name = "updated", IsRequired = false, EmitDefaultValue = false)]
        public RuleData[] UpdatedEntries { get; set; }

        [DataMember(Name = "deleted", IsRequired = false, EmitDefaultValue = false)]
        public int[] DeletedEntries { get; set; }
    }

此obj如果被发送,则正确反序列化

{
 TypeID: newOfferTypeId,
 Offers: requestOffers,
 NewEntries: self.newEntries.map(iPrepareEntry),
 UpdatedEntries: self.updatedEntries.map(iPrepareEntry),
 DeletedEntries: self.deletedEntries
};

这不是

{
type: newOfferTypeId,
off: requestOffers,
created: self.newEntries.map(iPrepareEntry),
updated: self.updatedEntries.map(iPrepareEntry),
deleted: self.deletedEntries
};

0 个答案:

没有答案