WCF-包含数组的Post JSON对象

本文关键字:JSON 对象 Post 包含 数组 WCF- | 更新日期: 2023-09-27 18:29:07

我正在尝试将JSON发布到WCF服务。json对象包含一个属性和一个数组。问题是,当请求到达服务器时,TripleTransaction总是为null。我想知道如何正确绑定到我的数据合约。如果有人能在这里给我一个指针,我将非常感激。

这就是我的服务界面:

 [OperationContract]
        [CorsBehavior]
        [WebInvoke(Method = "POST", UriTemplate = "Triples/{library}", ResponseFormat = WebMessageFormat.Json)]
        ResultMessage InvokeGraphTransactions(string library, TripleTransaction tripleTransaction);

我正试图用POSTed JSON填充TripleTransaction。TripleTransaction包含TripleModel的列表。

 [DataContract]
public class TripleTransaction
{
    [DataMember]
    public string Localization { get; set; }
    [DataMember]
    public List<TripleModel> Transactions { get; set; } 
}

 [DataContract]
   public class TripleModel
    {
        [DataMember]
        public int TransactionType { get; set; }
        [DataMember]
        public string SubjectLocalPart { get; set; }
        [DataMember]
        public string PredicateLocalPart { get; set; }
        [DataMember]
        public string ObjectPart { get; set; }
        [DataMember]
        public string Update { get; set; }
    }

上面的数据契约是像POSTed JSON一样建模的。使用FireBug,我可以看到请求看起来是正确的。JSON在消息有效负载中,请求标头具有contentType:application/JSON

这是我试图发送的json的一个例子:

 {
  "tripleTransaction": {
    "Localization": "en-us",
    "Transactions": [
      {
        "TransactionType": 0,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1",
        "Update": "2"
      },
      {
        "TransactionType": 1,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1"
      }
    ]
  }
}

WCF-包含数组的Post JSON对象

您应该发布以下内容:

 {
    "Localization": "en-us",
    "Transactions": [
      {
        "TransactionType": 0,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1",
        "Update": "2"
      },
      {
        "TransactionType": 1,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1"
      }
    ]
  }

在POST变量名称"tripleTransaction"下

否则,它不知道应该将数据链接到哪个参数