WCF RESTful JSON Web服务发布嵌套的对象列表

时间:2011-02-22 21:32:36

标签: asp.net json wcf

我正在开发一个接受JSON参数的WCF服务。我无法弄清楚我哪里出错了。请帮忙。

当我使用fiddler测试服务时,我发布以下内容:

"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);

[UPDATE]

这是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;
}

我错过了什么?

1 个答案:

答案 0 :(得分:3)

问题是你的JSON。

你还没有分享班级LocationData所以我不能告诉你它应该是什么样子但是你的JSON需要用括号括起来:

{
    "locations": {
        "Departments": [{
            "Name": "Amazonas",
            "alias": "",
            "Municipalities": [{
                "Name": "El Encanto"
            }, {
                "Name": "La Chorrera"
            }]

        }]
    }
}

我猜LocationData实际上是Departments "locations":,所以我认为{ "Departments": [{ "Name": "Amazonas", "alias": "", "Municipalities": [{ "Name": "El Encanto" }, { "Name": "La Chorrera" }] }] } 是多余的:

{{1}}