码迷,mamicode.com
首页 > 编程语言 > 详细

url参数用&拼接并且按照字母顺序排序方法

时间:2015-04-22 20:07:24      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

 private string urlParamManager(JObject postArray, bool flag)
        {
            ArrayList keys = new ArrayList();
            foreach (JToken child in postArray.Children())
            {
                JProperty property = child as JProperty;
                if (flag)
                {
                    if (!"signMethod".Equals(property.Name) && !"signature".Equals(property.Name))
                    {
                        keys.Add(property.Name);
                    }
                }
                else
                {
                    keys.Add(property.Name);
                }
            }
            keys.Sort();
            StringBuilder sb = new StringBuilder();
            foreach (string key in keys)
            {
                if (flag)
                {
                    if (!string.IsNullOrEmpty(postArray[key].Value<string>()))
                    {
                        if (string.IsNullOrEmpty(sb.ToString()))
                        {
                            sb.Append(key + "=" + postArray[key].Value<string>());
                        }
                        else
                        {
                            sb.Append("&" + key + "=" + postArray[key].Value<string>());
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(sb.ToString()))
                    {
                        sb.Append(key + "=" + postArray[key].Value<string>());
                    }
                    else
                    {
                        sb.Append("&" + key + "=" + postArray[key].Value<string>());
                    }
                }
            }
            return sb.ToString();
        }

调用方式:

 JObject param = new JObject();
 param["transaction"] = transaction;
 param["payType"] = payType;
param["userId"] = userId;
string paramStr = this.urlParamManager(param, true);

 

url参数用&拼接并且按照字母顺序排序方法

标签:

原文地址:http://www.cnblogs.com/yonguibe/p/4448326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!