繁体   English   中英

使用.net和C#反序列化Webhook的json响应

[英]Deserialize a json response form a Webhook using .net and C#

我一直在尝试了解去纤毛的工作原理,但没有成功。 我已经尝试了许多示例,但无法弄清楚如何反序列化下面的代码。 这是来自Webhook的json响应。 任何帮助,将不胜感激。

[
  {
    "msys": {
      "message_event": {
        "type": "bounce",
        "bounce_class": "1",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "device_token": "45c19189783f867973f6e6a5cca60061ffe4fa77c547150563a1192fa9847f8a",
        "error_code": "554",
        "ip_address": "127.0.0.1",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "msg_from": "sender@example.com",
        "msg_size": "1337",
        "num_retries": "2",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "raw_reason": "MAIL REFUSED - IP (17.99.99.99) is in black list",
        "reason": "MAIL REFUSED - IP (a.b.c.d) is in black list",
        "routing_domain": "example.com",
        "subject": "Summer deals are here!",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138"
      }
    }
  },
  {
    "msys": {
      "message_event": {
        "type": "spam_complaint",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "fbtype": "abuse",
        "friendly_from": "sender@example.com",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "report_by": "server.email.com",
        "report_to": "abuse.example.com",
        "subject": "Summer deals are here!",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138",
        "user_str": "Additional Example Information"
      }
    }
  },
  {
    "msys": {
      "message_event": {
        "type": "out_of_band",
        "bounce_class": "1",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "device_token": "45c19189783f867973f6e6a5cca60061ffe4fa77c547150563a1192fa9847f8a",
        "error_code": "554",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "msg_from": "sender@example.com",
        "rcpt_to": "recipient@example.com",
        "raw_reason": "MAIL REFUSED - IP (17.99.99.99) is in black list",
        "reason": "MAIL REFUSED - IP (a.b.c.d) is in black list",
        "routing_domain": "example.com",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822
      }
    }
  },
  {
    "msys": {
      "track_event": {
        "type": "click",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "ip_address": "127.0.0.1",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "target_link_name": "Example Link Name",
        "target_link_url": "http://example.com",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
        "geo_ip": {
          "country": "US",
          "region": "MD",
          "city": "Columbia",
          "latitude": "39.1749",
          "longitude": "-76.8375"
        }
      }
    }
  },
  {
    "msys": {
      "track_event": {
        "type": "open",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "ip_address": "127.0.0.1",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
        "geo_ip": {
          "country": "US",
          "region": "MD",
          "city": "Columbia",
          "latitude": "39.1749",
          "longitude": "-76.8375"
        }
      }
    }
  },
  {
    "msys": {
      "unsubscribe_event": {
        "type": "link_unsubscribe",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "mailfrom": "recipient@example.com",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"
      }
    }
  }
]

我的代码:

Message_Event jsonarrayMessage_Event = JsonConvert.DeserializeObject<Message_Event>(webhookdata); 
campaign_id = jsonarrayMessage_Event.campaign_id.ToString(); 
rcpt_to = jsonarrayMessage_Event.rcpt_to.ToString(); 
subject = jsonarrayMessage_Event.subject.ToString(); 

但我不断收到错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型'Message_Event',因为该类型需要JSON对象(例如{“ name”:“ value”})才能正确反序列化。要解决此错误,请更改将JSON转换为JSON对象(例如{“ name”:“ value”}),或将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为可从JSON数组反序列化的List 。 还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。

您需要实现一个类来保存数据

public class MessageEvent 
{
    public string type{ get; set; }
    public int bounce_class { get; set; }
    ... etc 
}

然后使用以下内容对其进行反序列化。

MessageEvent tmp = JsonConvert.DeserializeObject<MessageEvent>(json);

编辑:正如沃尔坎所说,您将需要一个外部库才能使用JsonConvert。 您可以在NuGet上找到它-搜索Newtonsoft.Json

您可以使用newtonsoft json.net块-根据您的json建立一个数据模型,并按照此处所述使用该模型:

http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm

掘金画廊

https://www.nuget.org/packages/Newtonsoft.Json/

您还可以使用Visual Studio的炫酷功能:将json复制到剪贴板(Ctrl + C),然后将其粘贴为C#类...

编辑->过去的特别->将Json粘贴为类

这是处理复杂的json对象的非常方便的方法

我的代码是:

webhookdata = TextBox1.Text.ToString().Trim(); 

Message_Event jsonarrayMessage_Event = JsonConvert.DeserializeObject<Message_Event>(webhookdata); 
campaign_id = jsonarrayMessage_Event.campaign_id.ToString(); 
rcpt_to = jsonarrayMessage_Event.rcpt_to.ToString(); 
subject = jsonarrayMessage_Event.subject.ToString(); 

我得到的错误是:

无法将当前JSON数组(例如[1,2,3])反序列化为类型'Message_Event',因为该类型需要JSON对象(例如{“ name”:“ value”})才能正确反序列化。要解决此错误,请更改将JSON转换为JSON对象(例如{“ name”:“ value”}),或将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为可从JSON数组反序列化的List 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM