Fork me on GitHub

.Net 获取URL中指定参数值

有时候我们调用第三方接口,返回的并不是完整的链接带参数,而我们只需要获取指定的一些参数

例如:code=200&msg=success&timestamp=1657539&nonce_str=T8Zhan&data=%7B%22pre_tre_no%22%3A%22OID2022071117311BA%22%2C%22open_ord_id%22%3A%22EP6155220700100032%22%2C%22open_ord_time%22%3A1657531889%2C%22open_uni_id%22%3A6155%2C%22open_app_id%22%3A%221118%22%2C%22open_acc_id%22%3A6155220100100002%2C%22open_pay_type%22%3A%22pay_yy%22%2Cb_pay_type%22%3A%22%22%2Cpen_status%22%3A9%2C%22open_type%22%3A1%2C%22open_user_percode%22%3A%22110180%22%n_fee_total%22%3A%220.02%22%2C%22ext_trade_no%22%312%2C%22ext_trade_time%22%3A1657531888%2C%22ext_mer_id%22%3A%221037%22%2C%22ext_wallet%22%3A1%2C%22pre_attach%22%3A%22%22%2C%22pre_ode%22%3A%22%22%2C%22pre_order_tus%22%3A3%7D&method=nify&real_app_id=1118&sign=8032BEBE4916DAA256C419B

 

除了傻瓜式解码然后再写一堆方法去截取参数之外,当然还有更好的写法!

 

var str = "code=200&msg=success&timestamp=1657531889&nonce_str=T88cZhan&data=%7B%22pre_trade_no%22%3A%22OID20220711173116DDBA%22%2C%22open_ord_id%22%3A%22EP61552232%22%2C%22open_ord_time%22%3A1657531889%2C%22open_uni_id%22%3A6155%22open_app_id%22%3A%28%22%2C%22open_acc_id%22%3A615522012%2C%22open_pay_type%22%3A%22pay_yay%22%2C%22open_sub_pay_type%22%3A%22C%22open_status%22C%22open_type%22%22open_user_percode%22%3%22%2C%22open_fee_total%22%3A%220.02%22%2C%22ext_trade_no%22%3A2%2C%22ext_trade_time%22%3A1688%2C%22ext_mer_id%22%3A%2210C%22ext_wallet_num%22%3A1%ttach%22%3A%22%22%2C%22pre_pay_code%22%3A%status%22%3A3%7D&method=noty&real_app_id=18&sign=8056C419B";
//var conStr = HttpUtility.UrlDecode(str);
var collection = HttpUtility.ParseQueryString(str);
var psrsms = JsonConvert.DeserializeObject<data>(collection["data"]);
//var collection2 = HttpUtility.ParseQueryString(conStr);
Console.WriteLine(psrsms.pre_trade_no);
//Console.WriteLine(collection2["data"]);
Console.ReadLine();




public class data
{
    public string pre_trade_no { get; set; }    //第三方商户订单号
    public string pre_attach { get; set; }      //原值返回字符串
    public string open_status { get; set; }     //订单支付状态,"9"代表已成功
    public string open_ord_id { get; set; }     //开放平台订单ID
    public string open_pay_type { get; set; }   //订单支付渠道
}

  

posted @ 2022-07-11 18:22  WantRemake  阅读(367)  评论(0编辑  收藏  举报