ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-19 08:38:27  阅读:405  来源: 互联网

标签:json-net json javascript c paypal


介绍

我正在为我的演示项目使用Paypal付款实现.当用户确认请求时,响应和请求以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; }
        }

问题

正常情况下,在“字符串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中反序列化吗?

编辑:杰森回应

{  
   "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将为“发现”您需要的所有属性提供很大的帮助.

标签:json-net,json,javascript,c,paypal
来源: https://codeday.me/bug/20191119/2034837.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有