简介
我正在为我的演示项目使用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"
}
]
}
如果有人对此问题有所了解,请提供帮助。我们将非常感谢您的帮助或参考。谢谢您的时间。
答案 0 :(得分:3)
API会为您提供c# object(code source),使用其属性,无需反序列化。您不需要再次转换为JSON,JSON.parse,序列化,反序列化。例如:
std::cin
智能感知将有助于“发现”您需要的所有属性。