2015-12-22 10 views
3

介绍正确的方式进行反序列化在C#中的PayPal响应

我用支付宝付款,实现了我的演示project.When用户确认请求工作,响应和请求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”接收,并成功地分析数据和反序列化also.But错误,同时建立

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":"[email protected]", 
     "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" 
     } 
    ] 
} 

如果什么人有这个问题的想法,请做help.Any样的帮助或引用将是您的时间appreciated.Thanks。

+1

您收到JSON,然后使用'JObject'反序列化它,并使用'ToString()'重新序列化它。然后使用'JavaScriptSerializer'再次反序列化它。哦...实际上,我们需要您的JSON示例来显示您应该使用的格式。而且你绝对不能使用'foreach'来遍历单个对象。 –

+0

@YeldarKurmangaliyev感谢您的回复,我会更新问题与答复json –

回答

3

该API为您提供了一个c# objectcode source),使用它的属性,没有任何反序列化。您不需要再转换为JSON,JSON.parse,序列化,反序列化。例如:

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

智能感知对于“发现”您需要的所有属性将有很大的帮助。

+0

感谢您的帮助,这是正确和最安全的方法。 –