在c#中反序列化贝宝响应的正确方法

本文关键字:方法 响应 反序列化 | 更新日期: 2023-09-27 18:27:38

简介

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

代码设置

操作数据被"解析"的

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语句不能对类型为的变量进行操作"ResponseMappingObject.Payer_Info",因为ResponseMappingObject.Payer_Info"不包含公共定义用于"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"
      }
   ]
}

如果有人知道这个问题,请帮忙。任何形式的帮助或参考都将不胜感激。谢谢你抽出时间。

在c#中反序列化贝宝响应的正确方法

API为您获取一个c#对象(代码源),使用其属性,没有任何反序列化内容。您不需要再次转换为JSON、JSON.parse、序列化、反序列化。例如:

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

Intellisense将极大地帮助您"发现"所需的所有属性。