gpt4 book ai didi

javascript - 在 C# 中反序列化 Paypal 响应的正确方法

转载 作者:行者123 更新时间:2023-11-29 10:37:28 25 4
gpt4 key购买 nike

简介

我正在为我的演示项目使用 paypal 支付实现。当用户确认请求时,响应和请求以 json 格式收到(正如你们大多数人所知)。

代码设置

Action 数据被“解析”的地方

string str = JObject.Parse(executedPayment.ConvertToJson()).ToString(Newtonsoft.Json.Formatting.Indented);
var payerInfo = new JavaScriptSerializer().Deserialize<ResponseMappingObject.Payer_Info>(str);

foreach(var item in payerInfo)
{
string abc = payerInfo.first_name;
string abc2 = payerInfo.last_name;
}

我为了舒适而添加的映射类

public class Payer_Info
{
public string email { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string payer_id { get; set; }
}

问题

正常情况下,“string str”中的数据接收和解析成功,反序列化也成功。但是构建时出错

foreach statement cannot operate on variables of type 'ResponseMappingObject.Payer_Info' because ResponseMappingObject.Payer_Info' does not contain a public definition for 'GetEnumerator'

问题

如果反序列化 json 响应的方式正确,如何解决这个问题?

如果安全的话,我们可以在 javascript 中反序列化吗?

编辑:Json 响应

{  
"id":"PAY-9C822419X38654121KZ4O27I",
"create_time":"2015-12-22T06:28:32Z",
"intent":"authorize",
"payer":{
"payment_method":"paypal",
"payer_info":{
"email":"suhail339-buyer@gmail.com",
"first_name":"test",
"last_name":"buyer",
"payer_id":"S75P265T8HXXY",
"phone":"4086197056",
"shipping_address":{
"recipient_name":"test buyer",
"line1":"1 Main St",
"city":"San Jose",
"country_code":"US",
"postal_code":"95131",
"state":"CA"
}
}
},
"cart":"0HD75068VV063304H",
"transactions":[
{
"related_resources":[
{
"authorization":{
"id":"7BM47750VM8619157",
"create_time":"2015-12-22T06:28:32Z",
"update_time":"2015-12-22T06:28:32Z",
"amount":{
"currency":"USD",
"total":"249.99",
"details":{
"shipping":"0.00",
"subtotal":"249.99",
"tax":"0.00"
}
},
"payment_mode":"INSTANT_TRANSFER",
"state":"authorized",
"protection_eligibility":"ELIGIBLE",
"protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"parent_payment":"PAY-9C822419X38654121KZ4O27I",
"valid_until":"2016-01-20T06:28:32Z",
"links":[
{
"href":"https://api.sandbox.paypal.com/v1/payments/authorization/7BM47750VM8619157",
"rel":"self",
"method":"GET"
},
{
"href":"https://api.sandbox.paypal.com/v1/payments/authorization/7BM47750VM8619157/capture",
"rel":"capture",
"method":"POST"
},
{
"href":"https://api.sandbox.paypal.com/v1/payments/authorization/7BM47750VM8619157/void",
"rel":"void",
"method":"POST"
},
{
"href":"https://api.sandbox.paypal.com/v1/payments/authorization/7BM47750VM8619157/reauthorize",
"rel":"reauthorize",
"method":"POST"
},
{
"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-9C822419X38654121KZ4O27I",
"rel":"parent_payment",
"method":"GET"
}
]
}
}
],
"amount":{
"currency":"USD",
"total":"249.99",
"details":{
"shipping":"0.00",
"subtotal":"249.99",
"tax":"0.00"
}
},
"description":"100 Pairs with all services",
"item_list":{
"shipping_address":{
"line1":"1 Main St",
"city":"San Jose",
"country_code":"US",
"postal_code":"95131",
"state":"CA"
}
}
}
],
"state":"approved",
"links":[
{
"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-9C822419X38654121KZ4O27I",
"rel":"self",
"method":"GET"
}
]
}

如果有人对这个问题有想法,请提供帮助。我们将不胜感激任何形式的帮助或引用。感谢您的宝贵时间。

最佳答案

API 为您提供 c# object ( code source ),使用它的属性,没有什么可以反序列化。你不需要再次转换成 JSON,JSON.parse,序列化,反序列化。例如:

var firstName = executedPayment.payer.payer_info.first_name;
var lastName = executedPayment.payer.payer_info.last_name;

Intellisense 将非常有助于“发现”您需要的所有属性。

关于javascript - 在 C# 中反序列化 Paypal 响应的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com