我需要在我的C#控制台应用程序中使用Restsharp将以下参数发送到AP
我在这里和其他站点上进行了查看,但没有发现我可以使用的任何内容。
这是原始代码的样子
{
"LicenceNumber":"511237P",
"CardNumber":"DB07067",
"ExternalID":"ID56758",
"Comments":"data uploaded via automated weekly process",
"Rules":"EU",
"Activities": [
{
"StartTime":"2019-04-14 09:00:00",
"Duration":"480",
"ActivityType":"Rest"
}
]
}
我需要做的是使用Restsharp request.AddAddParameter将StartTime,Duration和ActivityType添加到“活动”中,但是我不确定如何继续。
到目前为止,我的工作是:
static void PostRecord(string url)
{
url = url + "/" + MembershipNumber;
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + Token);
request.AddParameter("LicenceNumber", LicenceNumber);
request.AddParameter("CardNumber", CardNumber);
request.AddParameter("ExternalID", ExternalID);
request.AddParameter("Comments", Comments);
request.AddParameter("Rules", Rules);
request.AddParameter("Activities", "Activities");
}
任何帮助将不胜感激
******更新********** 经过更多调查后,我对代码进行了修改,但表示必须提供“活动”详细信息,以免其无法识别数组中的值
url = url + "/" + MembershipNumber;
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
var Activities= new Dictionary<string, object>();
Activities.Add("StartTime", "2019-04-14 09:00:00");
Activities.Add("Duration", "480");
Activities.Add("ActivityType", "Rest");
JsonObject o = new JsonObject();
foreach (var kvp in Activities)
{
o.Add(kvp);
}
JsonArray array = new JsonArray();
array.Add(o);
request.AddHeader("Authorization", "Bearer " + Token);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("LicenceNumber", LicenceNumber);
request.AddParameter("CardNumber", CardNumber);
request.AddParameter("ExternalID", ExternalID);
request.AddParameter("Comments", Comments);
request.AddParameter("Rules", Rules);
request.AddParameter("Activities", array.ToString());
IRestResponse response = client.Execute(request);
答案 0 :(得分:1)
创建一个对象,然后相应地为其分配值:
public class Activity
{
public string StartTime { get; set; }
public string Duration { get; set; }
public string ActivityType { get; set; }
}
public class RootObject
{
public string LicenceNumber { get; set; }
public string CardNumber { get; set; }
public List<Activity> Activities { get; set; }
}
您可以使用自动属性,可以从this等网站中生成自动属性
然后,您可以创建该类的实例并分配所需的所有值,如下所示:
url = url + "/" + MembershipNumber;
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + Token);
// This how you assign your values for the RootObject class
RootObject MyObject = RootObject();
MyObject.LicenceNumber = LicenceNumber;
MyObject.CardNumber = CardNumber;
// then for the activities class you can do the following
MyObject.Activities = new List<Activity>();
MyObject.Activities.Add(new Activity(){StartTime = "2019-04-14 09:00:00", Duration = "480",ActivityType = "Rest"});
string jsonString = JsonConvert.SerializeObject(MyObject);
request.AddParameter("application/json", jsonString, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);