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

 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);

 

posted @ 2015-04-22 18:44  沙漠绿洲uibe  阅读(1629)  评论(0编辑  收藏  举报