2016-08-30 61 views
0

我的顧慮:C# - 如何發送和閱讀JSON數據的郵政陣列WCF RESTful服務

  1. 是我下面的代碼是否正確?對於正在發送的輸入。

  2. 我嘗試使用SOAPUI和POST MAN進行本地調試,我的函數正在被打中,但輸入爲空 - 所以我想知道 - 如何發送帶有JSON數據的http post請求?

URL我試圖: http://localhost:49240/TranslationService.svc/DVRDatalist

類型:JSON(應用/ JSON) 原始數據:

{"DVRMigration":"[{「ProgramID」:」1111」,」ServiceID」:」1111」,」ChannelNo」:」11」,」TimeSlot」:」1111」},{「ProgramID」:」2222」,」ServiceID」:」2222」,」ChannelNo」:」22」,」TimeSlot」:」2222」},{「ProgramID」:」3333」,」ServiceID」:」3333」,」ChannelNo」:」33」,」TimeSlot」:」3333」},{「ProgramID」:」4444」,」ServiceID」:」4444」,」ChannelNo」:」44」,」TimeSlot」:」4444」}]"} 
  • 我需要樣品輸入保存在下面的輸入的代碼,並分割,以便我可以執行我的數據庫操作,例如:

    {「ProgramID」:」1111」,」ServiceID」:」1111」,」ChannelNo」:」11」,」TimeSlot」:」1111」}

    ,並形成最終的名單,並返回JSON響應

  • ====================

    下面是將被髮送到Web Service樣品輸入:

    [INPUT] => JSON數據

    {「DVR Migration」:[{「ProgramID」:」1111」,」ServiceID」:」1111」,」ChannelNo」:」11」,」TimeSlot」:」1111」},{「ProgramID」:」2222」,」ServiceID」:」2222」,」ChannelNo」:」22」,」TimeSlot」:」2222」},{「ProgramID」:」3333」,」ServiceID」:」3333」,」ChannelNo」:」33」,」TimeSlot」:」3333」},{「ProgramID」:」4444」,」ServiceID」:」4444」,」ChannelNo」:」44」,」TimeSlot」:」4444」}]} 
    

    下面是叔他示例輸出,我應該從Web服務接收回來:

    [OUTPUT] => JSON數據

    {"DVR Migration」:[{"ChannelNo」:"11","GuideStart":"00","ProgramID」:"1111","PvrID":"111","SeriesID":"1001","ServiceID":"1234","TableID":"11","Title":"ABCD","UpdateFlag":"False」},{"ChannelNo」:"22","GuideStart":"00","ProgramID」:"2222","PvrID":"111","SeriesID":"1001","ServiceID":"1234","TableID":"11","Title":"ABCD","UpdateFlag":"False」},{"ChannelNo」:"33","GuideStart":"00","ProgramID」:"3333","PvrID":"111","SeriesID":"1001","ServiceID":"1234","TableID":"11","Title":"ABCD","UpdateFlag":"False」},{"ChannelNo」:"44","GuideStart":"00","ProgramID」:"4444","PvrID":"111","SeriesID":"1001","ServiceID":"1234","TableID":"11","Title":"ABCD","UpdateFlag":"False」}]} 
    

    文件名:ITranslationService.cs

    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "DVRDatalist", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
    [return: MessageParameter(Name = "DVR Data List Migration")] 
    List<DataList> DVRDatalist(Migration migration); 
    

    文件名:TranslationService.cs

    public List<DataList> DVRDatalist(Migration migration) 
    { 
        List<DataList> response = new List<DataList>(); 
        var list = JsonConvert.DeserializeObject<List<Migration>>(Convert.ToString(migration)); 
        foreach (var row in list) 
        { 
         var datalist = new DataList(); 
         string pgrmId = row.ProgramID; 
         string svcid = row.ServiceID; 
         string cn = row.ChannelNo; 
         string ts = row.TimeSlot; 
         string pvrid = row.PvrID; 
         string tid = row.TableID; 
         DataSet dsdvrdata = dvr.Getdvrdata(pgrmId, svcid, cn, ts, pvrid, tid); 
         datalist.ServiceID = dsdvrdata.Tables[0].Rows[0]["ServiceID"].ToString(); 
         datalist.ProgramID = dsdvrdata.Tables[0].Rows[0]["ProgramID"].ToString(); 
         datalist.ChannelNo = dsdvrdata.Tables[0].Rows[0]["ChannelNo"].ToString(); 
         datalist.UpdateFlag = dsdvrdata.Tables[0].Rows[0]["UpdateFlag"].ToString(); 
         datalist.Title = dsdvrdata.Tables[0].Rows[0]["Title"].ToString(); 
         datalist.GuideStart = dsdvrdata.Tables[0].Rows[0]["GuideStart"].ToString(); 
         datalist.SeriesID = dsdvrdata.Tables[0].Rows[0]["SeriesID"].ToString(); 
         datalist.PvrID = pvrid; 
         datalist.TableID = tid; 
    
         response.Add(datalist); 
        } 
        return response; 
    } 
    

    數據成員:

    public class Migration 
    { 
        //string topstring = string.Empty; 
        //List<string>=new List<>; 
    
        public List<DVRMigration> DVRMigration { get; set; } 
        public string ChannelNo { get; set; } 
        public string ProgramID { get; set; } 
        public string PvrID { get; set; } 
        public string ServiceID { get; set; } 
        public string TableID { get; set; } 
        public string TimeSlot { get; set; } 
    } 
    
    public class DVRMigration 
    { 
        public string ServiceID { get; set; } 
        public string ProgramID { get; set; } 
        public string ChannelNo { get; set; } 
        public string TimeSlot { get; set; } 
        public string PvrID { get; set; } 
        public string TableID { get; set; } 
    } 
    
    public class DataList 
    { 
        #region Properties 
        [DataMember(Order = 1)] 
        public string ServiceID { get; set; } 
    
        [DataMember(Order = 2)] 
        public string ProgramID { get; set; } 
    
        [DataMember(Order = 3)] 
        public string ChannelNo { get; set; } 
    
        [DataMember(Order = 4)] 
        public string UpdateFlag { get; set; } 
    
        [DataMember(Order = 5)] 
        public string Title { get; set; } 
    
        [DataMember(Order = 6)] 
        public string GuideStart { get; set; } 
    
        [DataMember(Order = 7)] 
        public string SeriesID { get; set; } 
    
        [DataMember(Order = 8)] 
        public string PvrID { get; set; } 
    
        [DataMember(Order = 9)] 
        public string TableID { get; set; } 
    
        #endregion 
    } 
    

    回答

    0

    首先,您應該使用[DataContract]屬性來修飾您所需的請求/響應類(DataList類用於響應)。

    看着你的代碼,你的操作方法期待一個Migration對象,並且你想傳遞一個DVR Migration對象。在這種情況下,您應該嘗試將Action方法參數更改爲DVRMigration,或者您應該將JSON請求中的Migration對象傳遞給此操作方法。 MVC模型綁定通過綁定傳入的請求數據對象與Action參數來工作。它檢查這些的名稱和屬性兼容性。

    然後,您可以創建一個名爲DVRMigration JavaScript對象並初始化它的屬性,並將其發送到控制器即

    var input = { DvrMigration: { ServiceID: 1, ProgramID: 123 and so on} }; 
    $.ajax({ 
        type: "POST", 
        url: "http://localhost:49240/TranslationService.svc/DVRDatalist", 
        contentType: "application/json", 
        data: JSON.stringify(input), 
        success: function (result) { 
         alert("POST result: " + JSON.stringify(result)); 
        } 
    });