我需要创建一个具有子属性的动态请求,但我不完全确定该怎么做。 我安装了newtonsoft.json,所以我正在考虑制作一系列类,然后序列化它们,但我还必须传入凭据/消费者密钥Oauth的东西。
{
"type": "email",
"subject": "Creating a case via the API",
"priority": 4,
"status": "open",
"labels": [
"Spam",
"Ignore"
],
"message": {
"direction": "in",
"status": "received",
"to": "someone@desk.com",
"from": "someone-else@desk.com",
}
因此,要获得上面的请求,我将创建一个主类,该类将具有类型/主题/状态等属性,然后它也将包含我的其他类消息。我只是想弄清楚序列化如何转换类
愿这对你有所帮助
public class dataObj
{
public string type { get; set; }
public string subject { get; set; }
public int priority { get; set; }
public string status { get; set; }
public IList<string> labels { get; set; }
public IDictionary<string, string> message { get; set; }
}
private void testbutton_Click(object sender, EventArgs e)
{
try {
string json = @"{
""type"": ""email"",
""subject"": ""Creating a case via the API"",
""priority"": 4,
""status"": ""open"",
""labels"": [
""Spam"",
""Ignore""
],
""message"": {
""direction"": ""in"",
""status"": ""received"",
""to"": ""someone@desk.com"",
""from"": ""someone-else@desk.com""
}
}";
dataObj data = Newtonsoft.Json.JsonConvert.DeserializeObject<dataObj>(json);
}
catch {
}
}