反序列化MandrillApp网络挂接响应 [英] Deserializing MandrillApp Webhook response

查看:220
本文介绍了反序列化MandrillApp网络挂接响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MandrillApp API按说发送消息的JSON编码数组与MIME类型<$ 的C $ C>应用程序/ x-WWW窗体-urlencoded

MandrillApp API supposedly sends a JSON-encoded array of the messages with the mime type application/x-www-form-urlencoded.

我所遇到的问题是,接收到的数据,如长相:

The problem I have encountered is that the data received looks like:

mandrill_events=%5B%7B%22event%22%3A%22send%22
%2C%22msg%22%3A%7B%22ts%22%3A136510999...etc

URL解码,它是:

mandrill_events=[{"event":"send","msg":{
"ts":1365109999,"subject"...etc

我尝试这个字符串反序列化为表示JSON数据,但一类 JSON.NET 解串器吐出一个错误。

I try to deserialize this string into a class that represents the JSON data but the JSON.NET deserializer spits out an error.

代码

m = JsonConvert.DeserializeObject<MandrillEvents>(s);



例外

意外字符在解析值遇到:M。路径',行
0,位置0。

Unexpected character encountered while parsing value: m. Path '', line 0, position 0.

这是我有什么,我认为字符串mandrill_events = 部分是无效的。

From what I have I think that the mandrill_events= portion of the string is invalid.


  • 如果该字符串能够被反序列化?

  • 有没有,我很想念反序列化时?

  • 选项
  • 我应该如何看待呢?

推荐答案

mandrill_events = 不是有效的JSON。如果你把说出来,你应该能够解析它。

mandrill_events= is not valid JSON. If you take that out you should be able to parse it.

string validJson = originalText.Replace("mandrill_events=", ""); 
var m = JsonConvert.DeserializeObject<MandrillEvents[]>(validJson);

如果不行,我们需要看到更多的文本,以确定还有什么ISN T有效的JSON。 JSON是伟大的,因为它只有几个简单的数据类型。对象,数组,布尔,字符串,整数,等看到这个对所有有效类型, http://json.org/

If that doesn't work, we would need to see more of the text to determine what else isn't valid JSON. JSON is great because it only has a few simple datatypes. Object, Array, Boolean, String, Integer, etc. See this for all valid types, http://json.org/.

布赖恩罗杰斯提到你还反序列化到错误的类型(你有对象的数组,而不是一个单一的对象)。

As Brian Rogers mentioned you are also deserializing into the wrong type (you have an array of objects, not a single object).

如果您要检查如果整个JSON字符串是有效的,你可以使用

If you want to check if the entire json string is valid you can use

JArray array = JArray.Parse(validJson);

如果您不清楚可以使用

JToken token = JToken.Parse(validJson);

这两个对象,数组和价值观的作品。我仍然说反序列化到模型更好,因为你得到强类型,但是这至少可以保证你有有效的JSON你试图建立模型前。

Which works on both objects, arrays and values. I would still say deserializing into a model is better because you get strong typing, but this can at least assure you that you have valid JSON before you attempt to build the model.

的Visual Studio 2012有一个非常酷的功能:如果你复制有效的JSON文本,单击编辑> 粘贴为JSON类并它实际上将粘贴JSON作为一个有效的C#类,然后你知道你可以反序列化到。这使得构建在复杂的JSON更容易和比手动做容易出现错误少一个POCO模式。

Visual Studio 2012 has a VERY cool feature where if you copy the valid JSON text, click edit > paste as JSON class and it will actually paste the JSON as a valid C# class which you then know you can deserialize into. This makes building a POCO model from complex JSON much easier and less error prone than doing it manually.

在回答您的问题

您能够像他们
有哪些?

Are you able to comment on why they would send the response like they have?

我觉得很奇怪,并把客户端的更多的工作,能够解析/读他们的反应。您可以使用代表JSON几乎任何数据这就是为什么它是如此受欢迎的选择,因为用于Web API的数据交换格式。所有现代的语言有良好的JSON parers为好。这是普遍的,很好理解。当你拿出任何自定义格式发送,需要一个客户端的数据写自己的解析器甚至可以使用正则表达式,那么你重新发明轮子海事组织。在这种情况下,它们嵌入一种格式到另一个这仍然是相当丑陋,阻止你使用一个单一的解析器。

I think that is strange and puts more work on the client to be able to parse / read their response. You can represent almost any data with JSON which is why it's such a popular choice as a data-exchange format for web API's. All modern languages have good JSON parers as well. It's universal and well understood. When you come up with any custom format to send data that requires a client to write their own parser or even use regex then you are re-inventing the wheel IMO. In this case they are embedding one format into another which is still pretty ugly and stops you from using a single parser.

至于为什么,可能是为了向后兼容。他们暴露了他们为application /最初的X WWW窗体-urlencoded,不想打破现有的客户,使他们继续使用它,但嵌入JSON对象的API。从自己的文件(mandrillapp.com/api/docs/webhooks.JSON.html),貌似所有的反应是JSON,你可以仔细检查你使用了正确的网址,并您发送接受:在请求应用程序/ JSON头。这就是告诉你要回一个JSON响应的服务器的常用的HTTP方法。

As far as the WHY, it could be for backward compatibility. They exposed their API as application/x-www-form-urlencoded originally and didn't want to break existing clients so they continue using it but embed JSON objects. From their docs (mandrillapp.com/api/docs/webhooks.JSON.html), looks like all responses are JSON, you could double check that you are using the proper url and also that you send accept: application/json in the request header. This is the common HTTP way of telling the server you want back a JSON response.

这篇关于反序列化MandrillApp网络挂接响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆