我的顧慮:C# - 如何發送和閱讀JSON數據的郵政陣列WCF RESTful服務
是我下面的代碼是否正確?對於正在發送的輸入。
我嘗試使用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
}