繁体   English   中英

模型中的 JsonProperty 未使用

[英]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.

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