[英]JsonProperty in model not being used
我正在 Adyen Webhook 上構建一個 API 基礎,看起來 JsonProperty 沒有被“讀取”。
因此,我通過創建模型 NotifyRequest.cs 來構建概念證明。 對於我定義的屬性之一 [JsonProperty("NotificationItems")] 但通過 Postman 發布 JSON 時,該屬性為 NULL。 “Live”屬性正在被饋送。
通知請求.cs:
using Newtonsoft.Json;
using System.Collections.Generic;
using web_external_payments.Models;
namespace web_external_payments.Models
{
public class NotifyRequest
{
// public NotifyRequest();
public string Live { get; set; }
[JsonProperty("NotificationItems")]
public List<NotifyRequestItemContainer> NotifyItemContainers { get; set; }
//public string ToJson();
//public override string ToString();
}
}
控制器:
[HttpPost]
public ActionResult Webhook(NotifyRequest notificationRequest)
{
return View("Error");
}
JSON有效載荷:
{
"Live":"false",
"NotificationItems":[
{
"NotificationItem":{
"eventCode":"AUTHORISATION",
"success":"true",
"eventDate":"2019-06-28T18:03:50+01:00",
"merchantAccountCode":"YOUR_MERCHANT_ACCOUNT",
"pspReference": "7914073381342284",
"merchantReference": "YOUR_REFERENCE"
}
}
]
}
但是,如果我改為發布 JSON 而是使用屬性的名稱 (NotifyItemContainer),它會獲取該 JSON 中的值並且不再為 NULL。
我在 .NET 4.7.2 上。 這個特定的 .NET 版本是否有一些東西沒有使用 JsonProperty 或者我完全錯過了一些東西?
你將不得不根據 json 修復你的 NotifyRequest 類
public partial class NotifyRequest
{
[JsonProperty("Live")]
public bool Live { get; set; }
[JsonProperty("NotificationItems")]
public List<NotificationItemElement> NotificationItems { get; set; }
}
public partial class NotificationItemElement
{
[JsonProperty("NotificationItem")]
public NotificationItem NotificationItem { get; set; }
}
public partial class NotificationItem
{
[JsonProperty("eventCode")]
public string EventCode { get; set; }
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("eventDate")]
public DateTimeOffset EventDate { get; set; }
[JsonProperty("merchantAccountCode")]
public string MerchantAccountCode { get; set; }
[JsonProperty("pspReference")]
public string PspReference { get; set; }
[JsonProperty("merchantReference")]
public string MerchantReference { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.