我试图反序列化发送到我的WCF服务的JSON对象,但我遇到的问题似乎无法解决。 JSON有效负载中只有一个变量被反序列化,而另一个变量经常null
。我不确定为什么会这样,我无法正确反序化这两个变量。
我有以下内容:
通过Postman发送的JSON /请求:
POST /work/execute HTTP/1.1
Host: localhost
[{"id": "1234", "quantity": "3"}, {"id": "1234567", "quantity": "1"}]
WCF:
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "work/execute")]
WorkResponse DoWork(Stream str)
{
StreamReader rdr = new StreamReader(str);
string body = rdr.ReadToEnd();
JavaScriptSerializer ser = new JavaScriptSerializer();
List<WorkRequest> reqList = ser.Deserialize<List<WorkRequest>>(body);
return new WorkResponse() { Input = reqList };
}
[DataContract]
public class WorkRequest
{
[DataMember(Order = 0, Name = "id")]
public string ID;
[DataMember(Order = 1, Name = "quantity")]
public string Quantity;
}
JavaScriptSerializer
能够获得两个WorkRequest
个对象,但一旦完成,ID
的值反序列化为null
,Quantity
正确反映传入的值。我缺少什么?