【发布时间】:2013-11-20 10:10:52
【问题描述】:
将 2 个对象序列化为流并附加到 HttpWebRequest
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(OBJECT1));
ser.WriteObject(stream1, controller);
ser = new DataContractJsonSerializer(typeof(OBJECT2));
ser.WriteObject(stream1, reader);
stream1.position =0;
我的 json 字符串看起来像这样(在上面的代码之后):
{\"Object1Parameter1\":\"2\"}{\"Object2Parameter1\":\"Added reader\",\"Object2Parameter2\":\"2\"}
甚至尝试将名称分别为 OBJECT1 和 OBJECT2 的父级添加到此字符串中,但仍然没有在服务器上反序列化数据。
在Rest服务接收这样的数据是没有问题的
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Controller")]
string AddBothObjects(/*parameters of both objects listed*/);
如何在服务端以两个对象而不是单个参数接收此数据。 像这样的:
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Controller")]
string AddBothObjects(Object1 obj1,Object2 obj2);
【问题讨论】:
-
这个问题类似于stackoverflow.com/questions/15804588/… 但是,没有对该问题的回应这就是为什么作为一个单独的问题发布,其中包含有关我的方案的更多详细信息