在 C# 中反序列化贝宝响应的正确方法
Posted
技术标签:
【中文标题】在 C# 中反序列化贝宝响应的正确方法【英文标题】:Correct way to Deserialize paypal response in c# 【发布时间】:2016-03-28 07:55:31 【问题描述】:简介
我正在为我的演示项目使用 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;
问题
通常,在“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"
]
如果有人对此问题有任何想法,请提供帮助。任何形式的帮助或参考将不胜感激。感谢您的宝贵时间。
【问题讨论】:
您收到 JSON,然后使用JObject
对其进行反序列化,然后使用 ToString()
再次对其进行序列化。然后使用JavaScriptSerializer
再次反序列化它。哦...实际上,我们需要您的 JSON 示例来显示您应该使用什么格式。而且您绝对不能使用 foreach
遍历单个对象。
@YeldarKurmangaliyev 感谢您的回复,我将使用响应 json 更新问题
【参考方案1】:
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 将极大地帮助您“发现”您需要的所有属性。
【讨论】:
感谢您的帮助,这是正确和最安全的方法。+1 帮助以上是关于在 C# 中反序列化贝宝响应的正确方法的主要内容,如果未能解决你的问题,请参考以下文章
jQuery/ASMX:在 C# 中反序列化 JSON 时出错