C# - 如何发送和读取JSON数据的后置数组到WCF RESTful服务

时间:2016-08-30 22:41:59

标签: c# json wcf post

我的担忧:

  1. 我的下面的代码是否正确?正在发送的输入。

  2. 我尝试使用SOAPUI和POST MAN进行本地调试,我的函数被命中但输入为空 - 所以我想知道 - 如何使用JSON数据发送http post请求?

  3. 我正在尝试的网址: http://localhost:49240/TranslationService.svc/DVRDatalist

    类型:JSON(application / 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”}]"}
    
    1. 我需要保存输入的以下输入的示例代码,并拆分以便我可以执行我的数据库操作,例如:

      {“ProgramID”:”1111”,”ServiceID”:”1111”,”ChannelNo”:”11”,”TimeSlot”:”1111”}

      并形成一个最终列表并在json

    2. 中返回响应

      ====================

      以下是将发送到Web服务的Sample输入:

      [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 Service收到的示例输出:

      [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
      }
      

1 个答案:

答案 0 :(得分:-1)

首先,你应该用[DataContract]属性装饰你想要的请求/响应类(这里的DataList类用于响应)。

查看代码,您的操作方法需要一个Migration对象,并且您希望传递一个DVR Migration对象。在这种情况下,您应尝试将Action方法参数更改为DVRMigration,或者应将Migration对象从JSON请求传递给此操作方法。 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));
    }
});