【问题标题】:WCF RESTful JSON web service post Nested List of objectWCF RESTful JSON Web 服务发布嵌套对象列表
【发布时间】:2011-02-22 21:32:36
【问题描述】:

我正在开发一个接受 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);

[更新]

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

我错过了什么?

【问题讨论】:

标签: asp.net json wcf


【解决方案1】:

问题在于您的 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"
        }]

    }]
}

【讨论】:

  • 谢谢!。如果您愿意看一下,我已经在上面发布了 LocationData 类
  • 第一个 sn-p 成功了!第二个失败了。问题似乎是省略了外花括号。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多