c# -如何发送和读取Post数组JSON数据到WCF RESTful Service

本文关键字:数据 JSON WCF Service RESTful 数组 Post 何发送 读取 | 更新日期: 2023-09-27 18:04:44

我的关注点:

  1. 是我下面的代码正确吗?

  2. 我试着用SOAPUI和POST MAN在本地调试,我的功能被击中,但输入是空的-所以我想知道-如何发送一个http POST请求与JSON数据?

URL我正在尝试: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”}]"}
  • 我需要下面输入的示例代码,该输入被保存,并分割,以便我可以执行我的DB操作,例如:

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

    返回json
  • 格式的响应。

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

    下面是将被发送到Web服务的示例输入:

    [INPUT] => JSON Data

    {“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 Data

    {"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
    }
    

    c# -如何发送和读取Post数组JSON数据到WCF RESTful Service

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

    看看你的代码,你的动作方法期待一个迁移对象,你想传递一个DVR迁移对象。在这种情况下,您应该尝试将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));
        }
    });