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

问题描述:

我一直在试图了解deseriliazion如何工作,但没有成功。我已经尝试了很多例子,但无法弄清楚如何反序列化下面的代码。这是一个来自Webhook的json响应。任何帮助,将不胜感激。使用.net和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": "[email protected]", 
     "msg_size": "1337", 
     "num_retries": "2", 
     "rcpt_meta": { 
      "customKey": "customValue" 
     }, 
     "rcpt_tags": [ 
      "male", 
      "US" 
     ], 
     "rcpt_to": "[email protected]", 
     "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": "[email protected]", 
     "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e", 
     "rcpt_meta": { 
      "customKey": "customValue" 
     }, 
     "rcpt_tags": [ 
      "male", 
      "US" 
     ], 
     "rcpt_to": "[email protected]", 
     "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": "[email protected]", 
     "rcpt_to": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e", 
     "rcpt_meta": { 
      "customKey": "customValue" 
     }, 
     "rcpt_tags": [ 
      "male", 
      "US" 
     ], 
     "rcpt_to": "[email protected]", 
     "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)的类型,如List,可以反序列化fr om JSON数组。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。

+0

如果您发布迄今为止已尝试过的代码,那么在代码上构建起来会更容易。 –

+0

我的代码是: webhookdata = TextBox1.Text.ToString()。Trim(); Message_Event jsonarrayMessage_Event = JsonConvert.DeserializeObject (webhookdata); campaign_id = jsonarrayMessage_Event.campaign_id.ToString(); rcpt_to = jsonarrayMessage_Event.rcpt_to。的ToString(); subject = jsonarrayMessage_Event.subject.ToString(); – user2970010

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

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

+0

如果OP不熟悉,此解决方案需要一个名为Newtonsoft的外部库.Json –

+0

我的代码是: webhookdata = TextBox1.Text.ToString()。Trim(); Message_Event jsonarrayMessage_Event = JsonConvert.DeserializeObject (webhookdata); campaign_id = jsonarrayMessage_Event.campaign_id.ToString(); rcpt_to = jsonarrayMessage_Event.rcpt_to.ToString(); subject = jsonarrayMessage_Event.subject.ToString(); – user2970010

+0

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

您可以使用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非常方便的方式对象

+0

哇。感谢您的方便的复制功能! –

我的代码是:

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)的数组或类型,如List,可以从JSON数组中反序列化。