我正在尝试实施新的GoCardless API,但我在处理webhook时遇到了问题。
我通过以下方式阅读了对字符串的响应:
Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);
string requestContent = new StreamReader(req).ReadToEnd();
这给了我一个json响应如下:
"{\"events\":[{\"id\":\"EV000SDG4B5WRP\",\"created_at\":\"2017-07-31T08:17:16.202Z\",\"resource_type\":\"mandates\",\"action\":\"cancelled\",\"links\":{\"mandate\":\"MD0002010AE1MV\"},\"details\":{\"origin\":\"api\",\"cause\":\"bank_account_closed\",\"description\":\"The customer's account was disabled at your request.\"},\"metadata\":{}}]}"
根据文档,我应该能够做到
JsonConvert.DeserializeObject<GoCardless.Resources.Event>(requestContent);
但是,这总是给我一个空白对象,所有属性都设置为null。
可以在此处找到Event类的源代码:https://github.com/gocardless/gocardless-dotnet/blob/master/GoCardless/Resources/Event.cs
为什么这不会反序列化到对象?
答案 0 :(得分:4)
非常确定JSON是一系列事件。首先,您需要一个根对象:
public class Root
{
public List<GoCardless.Resources.Event> Events { get; set; }
}
现在反序列化为该类型:
var events = JsonConvert.DeserializeObject<Root>(requestContent);