使用子属性构建动态请求

时间:2014-01-14 21:56:15

标签: c# json formatting json.net

我需要创建一个带有子属性的动态请求,我不完全确定如何做到这一点。我安装了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",
  }

因此,要获得上述请求,我是否会创建一个主类,它将具有类型/主题/状态等属性,然后它也会包含其他类消息。我只想弄清楚序列化如何转换类

1 个答案:

答案 0 :(得分:0)

这可以帮到你

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 { 

        }
    }