所以我使用WCF服务在数据库中插入数据,为此我将POST中的数据发送到我的WCF服务并使用DataContract收集它:
POST发送的JSON数据
{
"data":
{
"subsidiarySid":"0",
"storesid":"0",
"date":"2018-06-15",
"counters":
[
{
"nation":"France",
"count":"5"
},
{
"nation":"France",
"count":"5"
}
]
}
}
DataContract
[DataContract(Name ="data")]
public class Count
{
[DataMember(Name ="subsidiarySid")]
public string subsidiarySid;
[DataMember(Name ="storeSid")]
public string storeSid;
[DataMember(Name = "date")]
public string date;
[DataMember(Name = "counters")]
public IEnumerable<Counter> counters;
}
[DataContract]
public class Counter
{
[DataMember(Name = "nationalitySid")]
public string nationalitySid;
[DataMember(Name = "count")]
public string count;
}
界面中的方法
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "count_customers")]
string PostCount(Count count);
问题是当我尝试访问此对象时,我得到一个null ... 我的合同是错的还是我在其他地方乱了? 我也可以在json的开头摆脱“data {}”(就像havig直接在json的根目录下我的“subsidiarySid等”)?
答案 0 :(得分:0)
毕竟找到了我的方式,所以我发布它作为答案供将来参考。我所做的是从DataContract中获取字符串属性(在OperationContract中按名称获取它们,如下所示:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "count_customers")]
string PostCount(string subsidiarySid, string storeSid, string date, IEnumerable<Counter> counters);
和DataContract
[DataContract(Name = "counter")]
public class Counter
{
[DataMember(Name = "nationalitySid")]
public string nationalitySid;
[DataMember(Name = "count")]
public string count;
}
只需删除DataContract定义中的“Name”属性,我就可以省略不必要的“data:{}”图层。