【问题标题】:Conversion of JSON object into class members in WEB API Post Action MethodWEB API Post Action Method中JSON对象到类成员的转换
【发布时间】:2019-12-22 12:20:29
【问题描述】:

我想将以下 JSON 对象转换为类成员: {"notificationId":"29397081-d4ed-4d2a-a672-4e875eabf535", "eventType":"net.authorize.customer.paymentProfile.deleted", “事件日期”:“2019-08-15T15:36:34.7856727Z”, "webhookId":"183a9022-510b-4801-a50c-75ef7310844f", “有效载荷”: {"customerProfileId":1920340068, "entityName":"customerPaymentProfile", "id":"1833395942"}}

我尝试过使用: 命名空间事务_

Public Class Payload
    Public customerProfileId As String = ""
    Public entityName As String = ""
End Class

Public Class transaction
    Public notification As String = ""
    Public eventType As String = ""
    Public eventDate As DateTime
    Public webhookId As String = ""
    Public payload As Payload = Nothing
    End Sub
End Class

结束命名空间

没用。 请推荐

【问题讨论】:

    标签: c# asp.net json vba webhooks


    【解决方案1】:

    您也可以使用 MongoDB.Bson nuget 包。它比 Newtonsoft 更大,但可以轻松地将 JSON 中的 notificationId 映射到您的通知字符串

    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.Bson.Serialization;
    
    [BsonIgnoreExtraElements]
    public class Transaction
    {
       [BsonElement("notificationId")]
       public string notification = "";
       public string eventType = "";
       public DateTime eventDate;
       public string webhookId = "";
       public Payload payload = null;
    }
    
    var myJson =
     @"{""notificationId"":""29397081-d4ed-4d2a-a672-4e875eabf535"", ""eventType"":""net.authorize.customer.paymentProfile.deleted"", ""eventDate"":""2019-08-15T15:36:34.7856727Z"", ""webhookId"":""183a9022-510b-4801-a50c-75ef7310844f"", ""payload"": {""customerProfileId"":1920340068, ""entityName"":""customerPaymentProfile"", ""id"":""1833395942""}}";
    
    Transaction myTransaction = BsonSerializer.Deserialize<Transaction>(myJson);
    

    【讨论】:

      【解决方案2】:

      在 C# 中,您可以使用 Newtonsoft.Json nuget 包,该包具有 JsonConvert.DeserialzeObject(json) 方法。

      示例:

      using Newtonsoft.Json;
      
      public class Payload
      {
          public string customerProfileId;
          public string entityName;
      }
      
      public class Transaction
      {
          public string notification = "";
          public string eventType = "";
          public DateTime eventDate ;
          public string webhookId  = "";
          public Payload payload = null;
      }
      
      var myJson =  
       @"{""notificationId"":""29397081-d4ed-4d2a-a672-4e875eabf535"", ""eventType"":""net.authorize.customer.paymentProfile.deleted"", ""eventDate"":""2019-08-15T15:36:34.7856727Z"", ""webhookId"":""183a9022-510b-4801-a50c-75ef7310844f"", ""payload"": {""customerProfileId"":1920340068, ""entityName"":""customerPaymentProfile"", ""id"":""1833395942""}}";
      
      Transaction myTransaction = JsonConvert.DeserializeObject<Transaction>(myJson);
      
      

      【讨论】:

      • 哪个类对象可以处理通过 webhook 传递给我的 web api 的 JSON 对象?
      • 它仍然将交易设为空。你能建议吗?提前致谢。
      • 如果我在一个小型控制台应用程序中运行此代码来测试它是否有效。你确定你提供给 JsonConvert.DeserializeObject() 的输入是正确的吗?
      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 2017-05-26
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多