[英]Conversion of JSON object into class members in WEB API Post Action Method
I want to convert the following JSON object into class members: {"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"}} 我想将以下JSON对象转换为类成员:{“ 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“,”有效载荷“:{” customerProfileId“:1920340068,” entityName“:” customerPaymentProfile“,” id“ :“ 1833395942”}}
I have tried using: Namespace transaction_ 我尝试使用:命名空间transaction_
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
End Namespace 结束命名空间
Didn't work. 没用 Please suggest
请建议
In C#, you could use the Newtonsoft.Json nuget package, which has the method JsonConvert.DeserialzeObject(json). 在C#中,您可以使用Newtonsoft.Json nuget包,该包具有方法JsonConvert.DeserialzeObject(json)。
example: 例:
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);
you also could use MongoDB.Bson nuget package. 您还可以使用MongoDB.Bson nuget包。 its a biger then Newtonsoft but allows easy mapping between your notificationId in JSON to your notifikation string
它比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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.