我如何提取 PayPal 响应 C#
Posted
技术标签:
【中文标题】我如何提取 PayPal 响应 C#【英文标题】:How can i extract PayPal Response C# 【发布时间】:2014-09-04 05:23:00 【问题描述】:如何在 c# 中提取这些数据,以便获取我需要的所有元素:
"id": "PAY-6A414645MC669653MKPB2WCI", "create_time": "2014-07-14T10:03:53Z", "update_time": "2014-07-14T10:05:09Z", "intent": "sale", "payer": "payment_method": "paypal", "payer_info": "email": "severiano.testes@gmail.com", "first_name": "tester", "last_name": "tester", "payer_id": "MSQ6UB55W52N6", "shipping_address": "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "country_code": "GB", "postal_code": "W12 4LQ", "state": "West Midlands" , "transactions": [ "amount": "currency": "EUR", "total": "54.00", "details": "subtotal": "54.00" , "description": "Transaction Description", "item_list": "items": [ "quantity": "1", "name": "Project Submission (featured)", "price": "54.00", "currency": "EUR", "sku": "64866" ], "shipping_address": "recipient_name": "tester tester", "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "country_code": "GB", "postal_code": "W12 4LQ", "state": "West Midlands" , "related_resources": [ "sale": "id": "4VV61663EL511901P", "create_time": "2014-07-14T10:03:53Z", "update_time": "2014-07-14T10:05:09Z", "amount": "currency": "EUR", "total": "54.00" , "state": "pending", "parent_payment": "PAY-6A414645MC669653MKPB2WCI", "links": [ "href": "https://api.sandbox.paypal.com/v1/payments/sale/4VV61663EL511901P", "rel": "self", "method": "GET" , "href": "https://api.sandbox.paypal.com/v1/payments/sale/4VV61663EL511901P/refund", "rel": "refund", "method": "POST" , "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6A414645MC669653MKPB2WCI", "rel": "parent_payment", "method": "GET" ] ] ], "state": "pending", "links": [ "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6A414645MC669653MKPB2WCI", "rel": "self", "method": "GET" ]
我尝试发送到 jquery 函数但没有成功。
我也试过这个:
JObject.Parse(HttpContext.Current.Items["ResponseJson"]).GetValue("id");
这有效,但仅适用于一个值,而不是我想要的全部
【问题讨论】:
【参考方案1】:你必须像这样声明类
public class Rootobject
public string id get; set;
public DateTime create_time get; set;
public DateTime update_time get; set;
public string intent get; set;
public Payer payer get; set;
public Transaction[] transactions get; set;
public string state get; set;
public Link1[] links get; set;
public class Payer
public string payment_method get; set;
public Payer_Info payer_info get; set;
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;
public Shipping_Address shipping_address get; set;
public class Shipping_Address
public string line1 get; set;
public string line2 get; set;
public string city get; set;
public string country_code get; set;
public string postal_code get; set;
public string state get; set;
public class Transaction
public Amount amount get; set;
public string description get; set;
public Item_List item_list get; set;
public Related_Resources[] related_resources get; set;
public class Amount
public string currency get; set;
public string total get; set;
public Details details get; set;
public class Details
public string subtotal get; set;
public class Item_List
public Item[] items get; set;
public Shipping_Address1 shipping_address get; set;
public class Shipping_Address1
public string recipient_name get; set;
public string line1 get; set;
public string line2 get; set;
public string city get; set;
public string country_code get; set;
public string postal_code get; set;
public string state get; set;
public class Item
public string quantity get; set;
public string name get; set;
public string price get; set;
public string currency get; set;
public string sku get; set;
public class Related_Resources
public Sale sale get; set;
public class Sale
public string id get; set;
public DateTime create_time get; set;
public DateTime update_time get; set;
public Amount1 amount get; set;
public string state get; set;
public string parent_payment get; set;
public Link[] links get; set;
public class Amount1
public string currency get; set;
public string total get; set;
public class Link
public string href get; set;
public string rel get; set;
public string method get; set;
public class Link1
public string href get; set;
public string rel get; set;
public string method get; set;
那么你必须使用using Newtonsoft.Json;
来表示DeserializeObject
之类的
var Data = JsonConvert.DeserializeObject<Rootobject>("your Json string");
您可以使用Data
访问该属性
O/P 看起来像
【讨论】:
【参考方案2】:你只需要制作一个映射对象,然后反序列化 json
public class PaymentResponse
public string id get; set;
public DateTime create_time get; set;
public DateTime update_time get; set;
public string intent get; set;
public Payer payerget; set;
public class Payer
public string payment_method get;set;
public PayerInfo payer_info get;set;
public class PayerInfo
public string email get; set;
public string first_name get; set;
没有时间添加所有值,但如果您使用 json 解析器,您可以更轻松地查看响应。我更喜欢这个。
http://json.parser.online.fr/
然后简单地用下面的反序列化 json 响应
var paymentResponse = new javascriptSerializer().Deserialize<PaymentResponse>(jsonString);
【讨论】:
以上是关于我如何提取 PayPal 响应 C#的主要内容,如果未能解决你的问题,请参考以下文章
在 php 脚本中执行 curl 命令并从服务器获取响应并提取它