WCF RESTful JSON web service post Nested List of object
I'm developing a WCF service that accepts parameters in JSON. I cant' figure out where I'm going wrong. Please help.
When I test the service using fiddler, I post the following:
"locations": {
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
I get a 400 error with: "The server encountered an error processing the request. The exception message is 'OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'string'.'. See server logs for more details."
The stacktrace is:
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.ImmutableDispatchRunti开发者_如何学JAVAme.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)
Here is the contract:
[OperationContract(Name = "setFacilities")]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "/setFacilities")]
string SetFacilities(LocationData locations);
[UPDATE]
Here's the LocationData class
[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;
}
What I'm I missing?
The problem is your JSON.
You have not shared the class LocationData
so I cannot tell you how it must look like but your JSON needs to be wrapped in brackets:
{
"locations": {
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
}
I guess LocationData
is actually the one which has Departments
so I think "locations":
is redundant:
{
"Departments": [{
"Name": "Amazonas",
"alias": "",
"Municipalities": [{
"Name": "El Encanto"
}, {
"Name": "La Chorrera"
}]
}]
}
精彩评论