Asp.net WCF RESTful JSON web服务后嵌套对象列表
我正在开发一个WCF服务,它接受JSON中的参数。我想不出哪里出了问题。请帮忙 当我使用fiddler测试服务时,我发布了以下内容:Asp.net WCF RESTful JSON web服务后嵌套对象列表,asp.net,json,wcf,Asp.net,Json,Wcf,我正在开发一个WCF服务,它接受JSON中的参数。我想不出哪里出了问题。请帮忙 当我使用fiddler测试服务时,我发布了以下内容: "locations": { "Departments": [{ "Name": "Amazonas", "alias": "", "Municipalities": [{ "Name": "El Encanto" },
"locations": {
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
我收到一个400错误:“服务器在处理请求时遇到错误。异常消息为‘OperationFormatter遇到无效的消息正文。应找到名为‘type’和值为‘object’的属性。找到值‘string’。有关详细信息,请参阅服务器日志。”
堆栈跟踪是:
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
合同如下:
[OperationContract(Name = "setFacilities")]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "/setFacilities")]
string SetFacilities(LocationData locations);
[更新]
这是LocationData类
[Serializable]
public class LocationData
{
public IList<Department> Departments;
}
[Serializable]
public class Department
{
public string Name;
public string Alias;
public IList<Municipality> Municipalities;
}
[Serializable]
public class Municipality
{
public string Name;
}
[可序列化]
公共类位置数据
{
公共部门;
}
[可序列化]
公共课系
{
公共字符串名称;
公共字符串别名;
公营城市;
}
[可序列化]
公营市
{
公共字符串名称;
}
我遗漏了什么?问题在于你的JSON 您尚未共享类
LocationData
,因此我无法告诉您该类的外观,但您的JSON需要用括号括起来:
{
"locations": {
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
}
我猜位置数据
实际上是一个有部门的数据
,所以我认为位置:
是多余的:
{
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
我唯一的建议是观看有关创建和使用restful web服务的网络广播。这里有4个视频(页面底部)——@JonWillis谢谢你的建议。谢谢!。如果您愿意看一看的话,我已经在上面发布了LocationData类。第一个代码片段成功了!第二个失败了。问题似乎是外部大括号的遗漏。谢谢