如何在 Rest Services 中获取 Json 对象?

Posted

技术标签:

【中文标题】如何在 Rest Services 中获取 Json 对象?【英文标题】:How to get Json object in Rest Services? 【发布时间】:2014-07-11 05:51:53 【问题描述】:

我是 Rest Services 的新手。我正在使用 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 中 [数据合约] 公共类 TertiarySales [数据成员] 公共字符串 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;
        

    

[运营合同]

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

在 Service.cs 中

public bool PostTertiarySales(WCFlib.CompositeType.TertiarySales 数据)

         return true;
    

谢谢

【问题讨论】:

没有像我得到 Serial=null 一样在“数据”中获取 salesdata 的值 您的意思是您没有获得TertiaryData 类属性? 【参考方案1】:

也为 TertiaryData 类属性装饰 [DataMember]

[DataContract]
public class TertiaryData

    [DataMember]
    public string Serial  get; set; 
    ...

【讨论】:

以上是关于如何在 Rest Services 中获取 Json 对象?的主要内容,如果未能解决你的问题,请参考以下文章

[Angular Tutorial] 13 -REST and Custom Services

在 Spring Boot 应用程序中从 Angular JS 调用 Rest Services 时出错

如何将 JSON 传递给 REST API (IBKR)

如何从承诺中获取 JSON [重复]

如何使用 Retrofit @Query 调用 REST API

http://localhost:8399/arcgis/rest/services 无法访问,怎么办