如何在Rest服务中获取Json对象

本文关键字:获取 Json 对象 服务 Rest | 更新日期: 2023-09-27 18:04:31

我是Rest服务的新手。我正在使用Post方法传递Json对象,但我无法获得类属性的值。

我的代码如下

    function SaveBook() {
        var bookData = { "ISDCode": "TestISD", 
                "retailerCode":"RT148",
                 "count":"2",
                 "salesdata":[
                  {
                    "Serial":"3544334444",
                    "CustomerPhone":"98234234234",
                    "CustomerName":"Name1",
                    "CustomerInfoID":"1",
                    "TimeStamp":"22-May-14",
                    "Latitude":"10.3456",
                    "Longitude":"8.3453"
                   },
                   { 
                    "Serial":"35324432324",
                    "CustomerPhone":"9824322333333",
                    "CustomerName":"Name2",
                    "CustomerInfoID":"2",
                    "TimeStamp":"21-May-14",
                    "Latitude":"10.3344",
                    "Longitude":"8.9564"
                    }]
                    }
         $.ajax({
             type: "POST",
             url: "http://localhost/API/Service.svc/PostTertiarySales",
             data: JSON.stringify(bookData),
             contentType: "application/json",
             dataType: "json",
             //processData: true,
             success: function (data, status, jqXHR) {
                 alert("success..." + data);
             },
             error: function (xhr) {
                 alert(xhr.responseText);
             }
         });
     }
     </script>
在Iservice.cs

(DataContract)公共类tertiarsales{(数据成员)公共字符串ISDCode{获取;设置;}

        [DataMember]
        public string retailerCode { get; set; }
        [DataMember]
        public string count { get; set; }
        [DataMember]
        public List<WCFlib.CompositeType.TertiaryData> salesdata { get; set; }
    }
    [DataContract]
    public class TertiaryData
    {
        public string Serial { get; set; }
        public string CustomerPhone { get; set; }
        public string CustomerName { get; set; }
        public Int32 CustomerInfoID { get; set; }
        public DateTime TimeStamp { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public TertiaryData(string Serial, string CustomerPhone, string CustomerName, Int32 CustomerInfoID, DateTime TimeStamp, double Latitude, double Longitude)
        {
            this.Serial = Serial;
            this.CustomerPhone = CustomerPhone;
            this.CustomerName = CustomerName;
            this.CustomerInfoID = CustomerInfoID;
            this.TimeStamp = TimeStamp;
            this.Latitude = Latitude;
            this.Longitude = Longitude;
        }
    }
[OperationContract]

    [WebInvoke(UriTemplate = "PostTertiarySales",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, Method = "POST")]
    bool PostTertiarySales(WCFlib.CompositeType.TertiarySales data);
在Service.cs

public bool PostTertiarySales(wcflb . compositetype .)TertiarySales数据){

         return true;
    }

谢谢

如何在Rest服务中获取Json对象

为TertiaryData类属性装饰[DataMember]

[DataContract]
public class TertiaryData
{
    [DataMember]
    public string Serial { get; set; }
    ...
}