【问题标题】:JSON object has null array properties on WCF REST POSTJSON 对象在 WCF REST POST 上具有空数组属性
【发布时间】:2012-11-12 04:35:18
【问题描述】:

我正在将 JSON 对象发布到 WCF 中的 REST API。 string 和 int 属性很好,但 IList 类型的属性为 null,即使它们是在 JSON 中指定的。

这是 JSON 对象在字符串化后的样子:

{
    "Id":"1",
    "Name":"Group One ertqer",
    "Description":"This is group 1 estrfasd",
    "SecurityPrincipals":"[
        {
            "Id": "1",
            "DisplayName": "User One",
            "UserName": "user.one",
            "Sid": "null",
            "Fqdn": "gmd.lab" 
         } ,
        {
            "Id": "2",
            "DisplayName": "User Two",
            "UserName": "user.two",
            "Sid": "null",
            "Fqdn": "gmd.lab" 
         }
     ]",
    "SecurityPermissions":"[
        {
            "Id": "2",
            "Name": "Access Service Catalog"
        } ,
        {
            "Id": "1",
            "Name": "Access Portal"
        }
    ]"
}

这是我用来发布对象的 jQuery AJAX 代码:

var securitygroup = {
    group: {
        Id: $("#csm-security-groupid").val(),
        Name: $("#csm-security-groupname").val(),
        Description: $('#csm-security-groupdesc').val(),
        "SecurityPrincipals[]": principals,
        "SecurityPermissions[]": permissions
    }
};

$.ajax({
    url: 'http://localhost:809/RestDataServices/RestDataService.svc/SecurityGroups/SecurityGroup',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(securitygroup),
    dataType: 'json',
    type: 'POST',
    async: true,
    success: function (data, success, xhr) {
        alert('Group saved - ' + success);
    },
    error: function (xhr, status, error) {
        alert('Error! - ' + xhr.status + ' ' + error);
    }
});

这里是 REST 操作合约:

[OperationContract]
[WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "SecurityGroups/SecurityGroup")]
void SaveSecurityGroup(SecurityGroup group);

SecurityGroup 的数据合约:

[DataContract]
public class SecurityGroup
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public IList<SecurityPermission> SecurityPermissions { get; set; }

    [DataMember]
    public IList<SecurityPrincipal> SecurityPrincipals { get; set; }

}

最后,这是 REST 服务的 web.config 的相关部分:

  <system.serviceModel>
    <services>
      <service name="RestDataServices.RestDataService" behaviorConfiguration="DefaultBehavior">
        <endpoint address="" binding="webHttpBinding" contract="RestDataServices.IRestDataService" behaviorConfiguration="web"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
    <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
  </system.webServer>

所以我尝试了很多不同的方法:

1) 尝试了包装和未包装 2) 最初数据协定有 List 属性,而不是 IList,但显然这不起作用 3) 我在 StackExchange 上解决了大约十几个其他问题,但似乎都与我的问题无关。

我宁愿不必创建自定义格式化程序...我猜我只是做错了事情,结果会很简单。

对此的任何帮助将不胜感激。我已经为此花费了大约 6 个小时,我即将放弃并将数组本身作为单独的调用发布。

【问题讨论】:

    标签: jquery arrays json wcf rest


    【解决方案1】:

    好的,答案是我需要做两件不同的事情。

    1) 我在我的 JSON 数组分配中添加了引号和方括号,因为我收到了 400 - 没有它们的错误请求。这是我的空数组/列表属性的来源。

    var securitygroup = {
        group: {
            Id: $("#csm-security-groupid").val(),
            Name: $("#csm-security-groupname").val(),
            Description: $('#csm-security-groupdesc').val(),
            "SecurityPrincipals[]": principals, //bad
            "SecurityPermissions[]": permissions //bad
        }
    };
    

    2) 删除 SecurityPrincipals 和 SecurityPermissions 属性上的“”和 [] 后,我再次收到 400 - Bad Request。我注意到我的整数属性在它们周围有引号,所以我使用 parseInt 将它们强制为整数。我还强制将“null”设置为正确的 null 值。现在,构建 JSON 的代码如下所示:

    var principals = [];
    $("li[data-type='permission']").each(function () {
        principals.push(
            {
                Id: parseInt(this.id),
                DisplayName: this.getAttribute('data-name'),
                UserName: this.getAttribute('data-username'),
                Sid: this.getAttribute('data-sid') == "null" ? null : this.getAttribute('data-sid'),
                Fqdn: this.getAttribute('data-fqdn')
            }
        );
    });
    
    //permissions for the current group
    var permissions = [];
    $("input[type='checkbox']").each(function () {
        if (this.getAttribute("checked") == "checked") {
            permissions.push(
                {
                    Id: parseInt(this.getAttribute('data-id')),
                    Name: this.getAttribute('data-name')
                }
            );
        }
    });
    
    var securitygroup = {
        group: {
            Id: parseInt($("#csm-security-groupid").val()),
            Name: $("#csm-security-groupname").val(),
            Description: $('#csm-security-groupdesc').val(),
            SecurityPrincipals: principals,
            SecurityPermissions: permissions
        }
    };
    

    【讨论】:

      【解决方案2】:

      删除 [ 之前和 ] 之后的引号。

      JSON 中没有列表(或 iList?)。

      [] 表示一个数组。

      参考http://www.json.org/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多